How To Enable/disable Gps And Mobile Data In Android Programmatically?
I want my application to be able to enable/disable gps and mobile data programmatically as there are many apps like tasker, profile flow, lookout extra which can do this so I searc
Solution 1:
I want my application to be able to enable/disable gps
Fortunately, this is not possible, for obvious privacy reasons. While there were some hacks, like the one in your question, that used to work, those security flaws have since been fixed.
as there are many apps like tasker, profile flow, lookout extra which can do this
You are welcome to supply evidence that any of those apps can do this on modern versions of Android.
Solution 2:
Try this for turning on gps
publicvoidturnGPSOn()
{
Intentintent=newIntent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
this.ctx.sendBroadcast(intent);
Stringprovider= Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps")){ //if gps is disabledfinalIntentpoke=newIntent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
this.ctx.sendBroadcast(poke);
}
}
for turning off gps,
publicvoidturnGPSOff()
{
Stringprovider= Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps")){ //if gps is enabledfinalIntentpoke=newIntent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
this.ctx.sendBroadcast(poke);
}
}
For mobile data, did you added the permission in manifest? If not try adding and check it.
<uses-permissionandroid:name="android.permission.CHANGE_NETWORK_STATE"/>
Let me know if everything worked.
Post a Comment for "How To Enable/disable Gps And Mobile Data In Android Programmatically?"