Android Device Gps On/Off Programmatically

How can I enable or disable the GPS programmatically on Android?

the GPS can be toggled by exploiting a bug in the power manager widget. see this xda thread for discussion.

here's some example code i use

private void turnGPSOn(){
String provider = Settings.Secure.getString(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"));
sendBroadcast(poke);
}
}

private void turnGPSOff(){
String provider = Settings.Secure.getString(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"));
sendBroadcast(poke);
}
}

use the following to test if the existing version of the power control widget is one which will allow you to toggle the gps.

private boolean canToggleGPS() {
PackageManager pacman = getPackageManager();
PackageInfo pacInfo = null;

try {
pacInfo = pacman.getPackageInfo("com.android.settings", PackageManager.GET_RECEIVERS);
} catch (NameNotFoundException e) {
return false; //package not found
}

if(pacInfo != null){
for(ActivityInfo actInfo : pacInfo.receivers){
//test if recevier is exported. if so, we can toggle GPS.
if(actInfo.name.equals("com.android.settings.widget.SettingsAppWidgetProvider") && actInfo.exported){
return true;
}
}
}

return false; //default
}

Android - turn GPS on or off programmatically

There used to be a way to enable / disable GPS programmatically by sending the android.location.GPS_ENABLED_CHANGE broadcast:

Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", enabled);
sendBroadcast(intent);

where enabled would be true or false respectively.

If you take a look at this bug report, this hack was subverted in Android 4.4. It still works on older OS versions.

Now the answer to your question

Why we need to go setting for on/off GPS on the other hand we can
on/off WIFI and Bluetooth programmatically without move to settings ?

Android's GPS technology periodically sends location data to Google even when no third-party apps are actually using the GPS function. A lot of people are very sensitive about things like real-time location monitoring . That's why Google made it mandatory to get the user's consent before using the GPS function. The following dialog is seen whenever the user turns GPS on:

GPS user permission

And hence it is no longer possible to programmatically change the GPS settings, as by necessity it requires the user's permission. What the programmer can do is direct the user to the GPS settings by calling

startActivity(context, new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));

and let the user make a choice.

As an interesting point, if you try sending the GPS_ENABLED_CHANGE broadcast on the new OS versions, you get a

java.lang.SecurityException: Permission Denial: 
not allowed to send broadcast android.location.GPS_ENABLED_CHANGE

error. As you can see, its a SecurityException with a permission denial message.

Android device GPS on/off programmatically

From my personal experience, I am answering this,

  • The hack code you shown in the your question has been stopped working from Android version 4.4. Your will fire this Exception starting from Kitkat version java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE

  • The First answer's code will not work any more, it only display animated GPS icon in notification bar.

  • For The security purpose Google developer has block above both methods which were previously working fine.

  • Hence conclusion is that You can not programmatically start GPS On or Off.

How to programmatically turn on GPS on Android

You can't turn it on/off programatically due to security reasons. According to Change location settings docs:

If your app needs to request location or receive permission updates, the device needs to enable the appropriate system settings, such as GPS or Wi-Fi scanning. Rather than directly enabling services such as the device's GPS, your app specifies the required level of accuracy/power consumption and desired update interval, and the device automatically makes the appropriate changes to system settings.

So, the best thing to do is, if the user has disabled the GPS, then for better UX you can show a prompt and redirect the user to the Location activity like this:

MainActivity.Instance.StartActivity(new Intent(ActionLocationSourceSettings));

Where MainActivity.Instance is your current Activity.

Turning on and off GPS programmatically in all android version

I want to turn on/off GPS through code

There is "enabled/disabled", and there is "on/off". "Enabled/disabled" determines whether any app can get GPS fixes. "On/off" determines whether the device is trying to get GPS fixes right now. GPS can only be "on" if it is "enabled".

If you request locations from the GPS_PROVIDER, and GPS is enabled, the GPS radio will turn on. If you stop requesting locations from the GPS_PROVIDER, and no other app is requesting locations from the GPS_PROVIDER, the GPS radio will turn off. Depending on how you configure your request for locations (e.g., once an hour), the GPS radio may turn off and on to save battery life, even while your request is outstanding. Hence, Android developers do not worry much about "on/off" — developers just request locations, and the hardware takes care of powering on and off the GPS radio as needed.

Your link really refers to "enabled/disabled". Android removed the ability for apps to enable and disable GPS back in Android 1.5, for privacy reasons. Malware authors have continued to try to find ways to get around this, because malware authors are not interested in user privacy. The link you cited was an exploit for a flaw in Android that allowed apps to enable and disable GPS. That exploit was fixed years ago. I am not aware of a current exploit that allows apps to enable and disable GPS.

How can I enable disable GPS programmatically in Android?

Try using this code. It worked for me on all the versions.

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

String provider = Settings.Secure.getString(ctx.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.ctx.sendBroadcast(poke);

}
}
// automatic turn off the gps
public void turnGPSOff()
{
String provider = Settings.Secure.getString(ctx.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"));
this.ctx.sendBroadcast(poke);
}
}

Android GPS toggle on/off programmatically

The icon is controlled by whether any app is actively using GPS (e.g., has called requestLocationUpdates() for GPS_PROVIDER). You are welcome to control your app's use of GPS, but you otherwise have no means of affecting that icon from an app.

Programmatically turn on/off GPS on Android

No it is not possible. The closest you can get is firing an Intent that shows the GPS settings to the user. Please follow this guide: Guide for GPS intent Android



Related Topics



Leave a reply



Submit