0

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.

Community
  • 1
  • 1

1 Answers1

0

This is not possible, for obvious privacy reasons, except via root. The security holes you are trying to exploit have been closed for quite some time.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • never mind i'm sure the aplication valide on play store do that by magic –  Oct 11 '14 at 13:02
  • @MichelTremblaySt-Germain: "They is lot app on google play do that" -- feel free to name them. – CommonsWare Oct 11 '14 at 13:04
  • where i cant go to erase this topic and my profil i don't need to lost time here and lost some else time ? –  Oct 11 '14 at 13:11