0

How to set GPS status on when the app is set as Device administrator by user.

I'm using this method :

private void turnGPSOn() {
    Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
    intent.putExtra("enabled", true);
    getApplicationContext().sendBroadcast(intent);

    String provider = Settings.Secure.getString(getApplicationContext()
            .getContentResolver(),
            Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if (!provider.contains("gps")) { // if gps is disabled
        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"));
        this.getApplicationContext().sendBroadcast(poke);

    }
}

and getting this error at least on API-21:

java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE from pid=27737, uid=10464

please please please care about Device administrator permission that is enabled and don't tag the question as duplicated!

  • possible duplicate of [Android device GPS on/off programatically](http://stackoverflow.com/questions/22528984/android-device-gps-on-off-programatically) – QuinnG Jun 20 '15 at 05:29
  • @Nija: did you even read the situation of app's status? this is `Device administrator` app, so it would have more abilities than a normal one. – AlirezA-Android Jun 20 '15 at 05:42
  • The issue I linked follows through to: https://code.google.com/p/android/issues/detail?id=35924 – QuinnG Jun 20 '15 at 06:03

1 Answers1

2

android.location.GPS_ENABLED_CHANGE intent can only be broadcasted by system apps as it is a protected broadcast(means your app should be either signed with systemsignature or it should be a system app). Even if your app is selected as Device Admin app by user, it does not mean it is eligible to use system features. Device Admin app will get access to features that are exposed by DevicePolicyManager class. Some of the global settings and secure settings can be controlled using DevicePolicyManger class on Lollipop and above.

Control Secure Settings

Control Global Settings

siva
  • 1,850
  • 13
  • 14