user3300414
user3300414

Reputation: 13

How to turn off the GPS when Activity in minimize in Android?

public boolean onKeyDown(int keyCode, KeyEvent event){
     if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
      Context context = getActivity();
         String provider = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        if (provider.contains("gps")){  
        final Intent poke = new Intent();
        poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
        poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
        poke.setData(Uri.parse("3")); 
        context.sendBroadcast(poke);
        }     
     }
    return super.onKeyDown(keyCode, event);
}

i did some Code but it can not disable the GPS. if you have a code please share it, i want GPS turn off when app is minimize or Closed and when app is started it will Automatically turn on the GPS, I turned it on but cant Turn off..

Upvotes: 1

Views: 520

Answers (2)

Pararth
Pararth

Reputation: 8134

Enabling and Disabling the GPS is in the hands of the user. You can show him a Dialog to inform him about disabling the GPS. Keep two buttons on the dialog in that case - one for "Settings" and another one for "ok" or "cancel".
Something like:

public static void promptForGPS(
        final Activity activity)
    {
        final AlertDialog.Builder builder =
            new AlertDialog.Builder(activity);
        final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
        final String message = "Disable GPS....Message here"
            + " service to find current location.  Click OK to go to"
            + " location services settings to let you do so.";

        builder.setMessage(message)
            .setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface d, int id) {
                        activity.startActivity(new Intent(action));
                        d.dismiss();
                    }
            })
            .setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface d, int id) {
                        d.cancel();
                    }
            });
        builder.create().show();
    }  

Further ref:
Android activate gps with AlertDialog: how to wait for the user to take action?

Upvotes: 1

user3291365
user3291365

Reputation: 455

The code you are using was a security loophole in Android system. It was working previously. Since the developer has fix this thing, you can not start/stop GPS Programmatically.

Upvotes: 0

Related Questions