Wednesday, August 14, 2013

Enable Disable GPS programatically in Android

Enable Disable GPS programatically in Android

Diable GPS 
above Version android 3.0
      if(android.os.Build.VERSION.SDK_INT > 11){
final Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
        intent.putExtra("enabled", false);
        mContext.sendBroadcast(intent);
}

Below Version android 3.0
else{
String provider = Settings.Secure.getString(
mContext.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"));
mContext.sendBroadcast(poke);
}
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


Enable GPS


above Version android 3.0
      if(android.os.Build.VERSION.SDK_INT > 11){
final Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
        intent.putExtra("enabled", true);
        mContext.sendBroadcast(intent);
}

Below Version android 3.0
else{
String provider = Settings.Secure.getString(
mContext.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"));
mContext.sendBroadcast(poke);
}
}

catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();


}

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...