2016-09-28 65 views
2

这是我的代码。 (它使alertdialog移动设置) 但我只想启用GPS时,我“启用GPS”按钮单击。 和安卓总是启用按钮点击时,我运行的应用程序不只是运行一次?android GPS启用按钮点击

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(); 

回答

1

你不能只启用GPS,你可以用这个意图只开GPS设置:

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"); 
} 

}