huzefa p
huzefa p

Reputation: 109

programatically turn on/off gps in android versions 4.x & onwards

The below code works well in android versions 2.x.x. It successfully turns on gps in devices. But the problem is it doesn't work on android versions 4.x onwards.

I am looking to turn on gps programatically in versions 4.x onwards. Any help or solution ?

    String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        Utility.print("provider string " + provider);
        if (!provider.contains("gps")) { // if gps is disabled
            Utility.print("gps is disabled now enabled");
            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"));
            sendBroadcast(poke);
            Utility.print("gps is disabled now enabled 1");
        } else {
            Utility.print("gps is already enabled");
        }

Upvotes: 2

Views: 1598

Answers (2)

idiottiger
idiottiger

Reputation: 5177

from the code:

 <receiver android:name=".widget.SettingsAppWidgetProvider" android:label="@string/gadget_title">

and above the android 4.0 become to this:

 <receiver android:name=".widget.SettingsAppWidgetProvider"
            android:label="@string/gadget_title"
            android:exported="false"
            android:enabled="@bool/has_powercontrol_widget">

added the android:exported="false"

Whether or not the content provider can be used by components of other applications — "true" if it can be, and "false" if not. If "false", the provider is available only to components of the same application or applications with the same user ID. The default value is "true".

so, your application dont have some uid and components name with the setting app, so it didn't work .

Upvotes: 0

Colin Pickard
Colin Pickard

Reputation: 46653

Third party apps should not be able to change protected system settings. I imagine this is a bug that was fixed for android 4.0 - possibly this one:

http://code.google.com/p/android/issues/detail?id=7890

Upvotes: 1

Related Questions