按钮点击时启用Android GPS

时间:2016-09-28 11:27:38

标签: android gps alertdialog

这是我的代码。 (它使alertdialog移动设置) 但我只是想在启动GPS时启用GPS"启用GPS"按钮单击。 当我运行app而不只是运行一次时,android总是启用按钮点击吗?

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

3 个答案:

答案 0 :(得分:1)

您无法启用GPS,您只能使用此意图打开gps设置:

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

答案 1 :(得分: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"); 
 }

 }

答案 2 :(得分:0)

我认为您只需点击Google就可以打开GPS位置。

enter image description here

要提示用户修改位置设置的权限,请调用startResolutionForResult(Activity,int)。此方法会显示一个对话框,要求用户修改位置设置。 This link shows how to check the location settings, and how to call startResolutionForResult(Activity, int).