如何从Android中的位置开启或关闭响应?

时间:2017-10-05 08:03:35

标签: java android android-location locationmanager

在我的应用中,使用开关按钮打开和关闭位置。如何使用默认位置选项(ON或OFF)功能获取此开关按钮。我是新手,帮助我任何人。

1 个答案:

答案 0 :(得分:0)

试试这个

  • 如果您正在使用Location Listener,请尝试使用
//take AlertDialog globally
AlertDialog alert;
@Override
public void onProviderDisabled(String provider) 
 {
// Here you need to call alert of enabling location to get the update 
  location
   showSettingsAlert();
  }

@Override
public void onProviderEnabled(String provider) 
 {
 alert.dismiss();
}
 public void showSettingsAlert(){
try {
        LoginActivity.isGPSAlertShown=true;
        alert = new AlertDialog.Builder(mContext).create();
        alert.setTitle("GPS is settings");
        alert.setCancelable(false);
        alert.setMessage("GPS is not enabled. Do you want to go to settings menu?");
        alert.setButton(Dialog.BUTTON_POSITIVE, "Settings", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivity(intent);
                isFromSetting = true;
                LoginActivity.isGPSAlertShown=false;
            }
        });
        alert.show();
    }catch (Exception e){e.printStackTrace();}
}
相关问题