1

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.

Community
  • 1
  • 1
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157

3 Answers3

0

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

xgc1986
  • 868
  • 6
  • 8
  • can i change these two device settings from within my application only? – Elad Benda2 Apr 27 '14 at 07:28
  • it will change for al your applications installed, you can't change the location configuration for only one application – xgc1986 Apr 27 '14 at 07:29
  • no, i meant how can i change the global settings for all application installed but from within my application - silently - without taking the user to the settings page – Elad Benda2 Apr 27 '14 at 07:30
  • with the code I wrote, it takes you to the location settings page, and returns to your app once is changed, then the locations setting is changed for all applications. if you want to create your own locations configuration view (without using the defaults android) I don't think that you can achieve that, maybe asking for superuser permissions there is a way, but I don't know that – xgc1986 Apr 27 '14 at 07:35
  • long time ago was this method for the gps, but it was a bug https://code.google.com/p/android/issues/detail?id=7890 – xgc1986 Apr 27 '14 at 07:40
0

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

Community
  • 1
  • 1
Coderji
  • 7,655
  • 5
  • 37
  • 51
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

Anshul Kabra
  • 129
  • 17