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:
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.
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);
}
}
How to enable Location access programmatically in android?
Use below code to check. If it is disabled, dialog box will be generated
public void statusCheck() {
final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps();
}
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
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.
Related Topics
How to Send and Receive Data Sms Messages
What Is the Real Android Studio Gradle Version
How to Implement a View Holder
Kill Another Application on Android
How to Implement Filterable in Realmrecyclerviewadapter
Android Studio - Unable to Merge from Svn Branch
Unresolved Reference - Activity Does Not Recognize Synthetic Imports in Android Studio V4
How to Use Icons and Symbols from "Font Awesome" on Native Android Application
Android Viewpager with Bottom Dots
How Does the Mapping Between Android Resources and Resources Id Work
Add Ripple Effect to My Button with Button Background Color
Can an Android App Connect Directly to an Online MySQL Database
How to Catch Sigsegv (Segmentation Fault) and Get a Stack Trace Under Jni on Android
Illegalargumentexception: Navigation Destination Xxx Is Unknown to This Navcontroller
Adt 22.2 New App Wizard: Unsupported Template Dependency: Upgrade Your Android Eclipse Plugin