Android : android programmatically Turning on and off GPS in android 4.0 and up

on Saturday, October 11, 2014


i want turn on and off gps localisation in a activity a try every other topic sugestion and this is not working. This is not duplicate topic i look all the other.


example:


manifest:



<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.Manifest.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.Manifest.permission.ACCESS_COARSE_LOCATION" />


Activity:



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(activityname.this.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"));
activityname.this.sendBroadcast(poke);


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


when i try My application stop (crash)


source : Turning on and off GPS programmatically in android 4.0 and above?


i don't whant show dialog (select open localisation) i need do all programmatically (hiden)


Please help me. Thank you.


0 comments:

Post a Comment