Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 4.1.2 How to turn off GPS programmatically

Tags:

android

gps

adt

I am trying to switch on GPS by code. The following is the code I've used.

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

The code executing in OnReceive function. What am I doing wrong?

like image 417
GIKO Avatar asked Nov 18 '13 13:11

GIKO


2 Answers

Android Guidelines have changed above version 4.0. You cannot change GPS off on programmatically for versions above 4.0. However, you can change wifi off on programmatically

like image 120
Rahul Gupta Avatar answered Oct 05 '22 22:10

Rahul Gupta


How to turn off GPS programmatically

The GPS radio will be on so long as:

  • It is enabled
  • One or more apps are trying to obtain the user's location via GPS

You are welcome to have your app no longer try to obtain the user's location. However, whether the GPS is on is outside of your control.

I try to use this code. But nothing happening.

That is good, because the security flaw that you are trying to exploit has long been fixed.

Apps cannot enable or disable GPS programmatically, except perhaps on rooted devices. Please allow the user to do that.

like image 32
CommonsWare Avatar answered Oct 05 '22 23:10

CommonsWare