在“设置”中启用位置切换时,如何更新android应用程序?

在“设置”中启用位置切换时,如何更新android应用程序?,android,android-activity,gps,android-location,Android,Android Activity,Gps,Android Location,我的应用程序通过导航到“位置设置”活动提示用户打开位置服务。 现在,当用户在“设置”活动中切换“位置”选项时,如何签入活动代码 public void checkGPS() { LocationManager lm = (LocationManager) GlobalHome.this.getSystemService(Context.LOCATION_SERVICE); boolean gps_enabled = false; boolean network_enabl

我的应用程序通过导航到“位置设置”活动提示用户打开位置服务。 现在,当用户在“设置”活动中切换“位置”选项时,如何签入活动代码

public void checkGPS() {
    LocationManager lm = (LocationManager) GlobalHome.this.getSystemService(Context.LOCATION_SERVICE);
    boolean gps_enabled = false;
    boolean network_enabled = false;

    try {
        gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception ex) {
    }

    try {
        network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception ex) {
    }

    if (!gps_enabled && !network_enabled) {
        // notify user
        AlertDialog.Builder dialog = new AlertDialog.Builder(GlobalHome.this);
        dialog.setMessage(MessagesString.LOCATION_DIALOG_MESSAGE);
        dialog.setPositiveButton(MessagesString.LOCATION_DIALOG_POSTIVE_TEXT, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                // TODO Auto-generated method stub
                Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                GlobalHome.this.startActivity(myIntent);
                //get gps
            }
        });
        dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                // TODO Auto-generated method stub

            }
        });
        dialog.show();
    }

}

我想你正在寻找这个,希望它能解决你的问题

public class MainActivity extends Activity implements LocationListener {

    LocationManager locationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }


    @Override
    protected void onResume() {
        super.onResume();

        isLocationServiceActive();

    }


    private void isLocationServiceActive(){

        locationManager=(LocationManager)getSystemService(LOCATION_SERVICE);
        if(locationManager!=null){
            LocationProvider gpsLocationProvider=locationManager.getProvider(LocationManager.GPS_PROVIDER);
            LocationProvider networkLocationProvider=locationManager.getProvider(LocationManager.NETWORK_PROVIDER);

            if(gpsLocationProvider==null && networkLocationProvider==null){
                //device does'nt support location services
                showDeviceNotSupportLocationsDialog();
            }
            else if(networkLocationProvider!=null && locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
                this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000L, 100.0F, this);

            }
            else if(gpsLocationProvider!=null && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){

                this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000L, 100.0F, this);

            }
            else{
                showNoGpsDialog();
            }

        }


    }


    public void showNoGpsDialog()
    {
        AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
        localBuilder.setMessage(this.getResources().getString(R.string.gps_network_not_enabled));
        localBuilder.setPositiveButton(this.getResources().getString(R.string.open_location_settings), new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
            {
                Intent localIntent = new Intent("android.settings.LOCATION_SOURCE_SETTINGS");
                MainActivity.this.startActivity(localIntent);
            }
        });
        localBuilder.setCancelable(false);
        localBuilder.show();
    }


    public void showDeviceNotSupportLocationsDialog()
    {
        AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
        localBuilder.setMessage(this.getResources().getString(R.string.device_not_support_locations));
        localBuilder.setPositiveButton(this.getResources().getString(R.string.exit), new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
            {
                MainActivity.this.finish();
            }
        });
        localBuilder.setCancelable(false);
        localBuilder.show();
    }


    @Override
    public void onLocationChanged(Location location) {

        //location changed
    }


    @Override
    public void onProviderDisabled(String s) {

        //your code here

    }

    @Override
    public void onProviderEnabled(String s) {

        //your code here
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

        //your code here
    }



}

当我打开gps时,不会调用重写的方法onLocationChanged onProviderDisabled onStatusChanged。我正在使用startActivityForResult。但有更好的方法吗?返回活动后,您需要再次检查以确保状态,即用户实际已选择“作为位置选项”,如果选项处于活动状态,则可以使用onProviderDisabled onStatusChanged Overrided methodsAgreed进行跟踪。但如何处理这种情况用户从通知栏打开gps?当您通过通知或设置激活或停用位置时,将调用onProviderDisabled(字符串providerName)和onProviderEnabled(字符串providerName)