简体   繁体   English

如何从Android中打开或关闭位置获取响应?

[英]how to get the response from location on or off in android?

In my app, using switch button for location on and off. 在我的应用程序中,使用“开关”按钮打开和关闭位置。 How to fetch this switch button with default location option(ON or OFF) function. 如何使用默认位置选项(打开或关闭)功能获取此开关按钮。 I am new for this ,help me any one. 我是新来的,请帮助我。

Try this 尝试这个

  • if you're using Location Listener then try this 如果您使用的是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();}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM