سؤال

I'm trying to get the location of the user:

// getting GPS status

isGPSEnabled = locationManage.isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
            Log.e(MyLogger.TAG, "Non providers are enabled");

I need the user to set enable network location or turn on GPS.

How can i help the user do each of these options?

  1. open the device settings page and return to my activity on back press.

  2. change these two device settings from within my application only?

meaning opening a confirmation dialog that will enable these two settings.

هل كانت مفيدة؟

المحلول

well changing these settings in code is not possible anymore after 4.1 (they may be a workarounds to turn it on because on a bug see here but it won't work on 4.3 or 4.4 or you need rooted phones, what you can do is show a dialog to the user to go to settings to enable the GPS

public static void LocationAlertDialog(final Context context) {

    AlertDialog.Builder d = new AlertDialog.Builder(context);
    d.setTitle("No Providers");
    d.setMessage("Unfortunately, you don't have any providers to get your location, would you like to turn on your GPS?");

    d.setNegativeButton(context.getResources().getString(R.string.dialogCancel), new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
        }
    });

    d.setPositiveButton(context.getResources().getString(R.string.dialogAccept), new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            context.startActivityForResult(i, 0);
        }
    });

    d.show();
}

call the previous function in

if (!isGPSEnabled && !isNetworkEnabled) {
        LocationAlertDialog(this);
        Log.e(MyLogger.TAG, "Non providers are enabled");

then in your onActivityResult you can check code 0 if the GPS has been turned on or not

نصائح أخرى

startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);

With the latest location api you can prompt user to change location setting dynamically through setting dialog inside the application.

https://developer.android.com/training/location/change-location-settings.html

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top