2016-09-28 4 views
2

das ist mein Code. (es macht alertdialog, um Einstellungen zu verschieben) aber ich möchte nur GPS aktivieren, wenn ich "GPS aktivieren" klicken. und funktioniert Androide immer Button klicken, wenn ich app nicht nur einmal laufen lasse?android GPS aktivieren, wenn Knopf klicken

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setMessage("Your GPS is disabled! Would you like to enable it?") 
.setCancelable(false) 
.setPositiveButton("Enable GPS", 
new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int id) { 
     moveConfigGPS(); 
    } 
}).setNegativeButton("Do nothing", 
new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int id) { 
     dialog.cancel(); 
    } 
}); 

AlertDialog alert = builder.create(); 
alert.show(); 

Antwort

1

Sie können nicht nur GPS aktivieren, können Sie nur öffnen GPS-Einstellungen mit dieser Absicht:

Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
startActivity(intent); 
0
User these method to on GPS : 

    intent1 = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
    startActivity(intent1); 

and add these method to check GPS is on or Off ?? 
public void CheckGpsStatus(){ 

locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE); 

GpsStatus = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

if(GpsStatus == true) 
{ 
textview.setText("Location Services Is Enabled"); 
}else { 
textview.setText("Location Services Is Disabled"); 
} 

}