Method-1
Enable GPS
Intent intent = new
Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);
Disable GPS
Intent intent = new
Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);
Method-2
// If GPS
is OFF
private
void turnGPSOn(){
String
provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps")){
final
Intent intent = new Intent();
intent.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
intent.setData(Uri.parse("3"));
sendBroadcast(intent);
}
}
// If GPS
is ON
private
void turnGPSOff(){
String
provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps")){
final
Intent intent = new Intent();
intent.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
intent.setData(Uri.parse("3"));
sendBroadcast(intent);
}
}
List of devices on which GPS successfully turned on and
off by program
Model : Nexus S , OS Version : 2.3.3
Model : HTC Sensation, OS Version : 2.3.3
Model : LG Optimus one, OS Version : 2.3.3
Model : HTC Legend ,OS Version : 2.2
Model : Samsung Galaxy
Ace,OS Version : 2.3.3
List of devices on which we are getting problem while GPS turned on and off by program
Model : Nexus One , OS Version : 2.3.6
Model : Nexus S , OS Version : 2.3.6
Model : Samsung Galaxy Mini, OS Version : 2.3.4
Model : Sony Ericsson
EXPERIA NEO V, OS Version : 2.3.4
Hello,
ReplyDeleteI'm student and I'm currently developing an application that needs enabled the gps programmaticcaly. I tried your code(method 1) but it only activates the GPS icon, it's doesn't start the service.
I read that send a broadcast to all receiver gps, but how do you know if the location gps start?
That way not work on OS version 4.0 above, do you know how about fix it? Thanks
ReplyDelete