0

I want to give user dialog to set GPS on/off. Can this be done using the new SettingAPI? If so, how: I saw the code below, but not sure if it works for Android 2.3 devices via compatibility library or not? What versions of Android are supported. In particular does user need to be connected to net to use this service for GPS or will it work with settings on device directly no need for internet connection? Also does the dialog show just GPS settings or can user access other parts of settings? I'm trying to restrict to just GPS Settings.

LocationSettingsStates locationSettingsStates = locationSettingsResult.getLocationSettingsStates();

    if (!locationSettingsStates.isGpsPresent() || !locationSettingsStates.isGpsUsable()) {

 Status status = locationSettingsResult.getStatus();
 if (status.getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) {
 try {
 status.startResolutionForResult(StilActivity.this, REQUEST_CHECK_SETTINGS);
 } catch (IntentSender.SendIntentException th) {
 Log.e(TAG, "Error opening settings activity.", th);
          }
      }
 }
MuayThai
  • 441
  • 1
  • 5
  • 19

1 Answers1

1

Just call the function and then

   public void start() {
        mlocManager = (LocationManager) 
                getSystemService(Context.LOCATION_SERVICE);

        if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Toast.makeText(getApplicationContext(), "Response ==>" + "GPS is on",
                    Toast.LENGTH_LONG).show();
        } else {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    MainActivity.this);
            alertDialogBuilder
                    .setMessage("GPS is disabled in your device. Enable it?")
                    .setCancelable(false)
                    .setPositiveButton("Enable GPS",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    Intent callGPSSettingIntent = new Intent(
                                            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                    MainActivity.this.startActivity(callGPSSettingIntent);
                                }
                            });
            alertDialogBuilder.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = alertDialogBuilder.create();
            alert.show();

        }
    }

And just as a slight suggestion you could call the startActivityforResult() and the once the user comes back from the location page you can do your further execution.

Vaibhav Barad
  • 625
  • 8
  • 17
  • This does work. However once users get to the settings they can access all settings via back button right? I'm building Kiosk, and want them only to access the GPS setting. So I need something more restrictive. Clicking on the back button 'inside' location settings takes them to all settings. – MuayThai Sep 15 '15 at 03:51
  • As per this [post](http://stackoverflow.com/a/5305835/2713414) you could do it earlir because of a bug in the power manager widge, but as per the recent Android Guidelines changes above version 4.0. You cannot change GPS off on programmatically for versions above 4.0 due to some privacy reasons .. However, you can change wifi off on programmatically – Vaibhav Barad Sep 15 '15 at 05:12