2013-03-15 13 views
27

Ciao, sto creando un'applicazione antifurto e, localizzando il mio telefono tramite sms, funziona perfettamente fino al 2.3. Ma in 4.0 non posso accendere o spegnere gps programmaticamente c'è un altro modo possibile per accendere il GPS tramite codice.Come posso abilitare disabilitare il GPS programmaticamente in Android?

+0

http://stackoverflow.com/questions/4721449/enable-gps-programatically-like-tasker hanno guarda qui – Dilip

+2

pic passato di codice nella tua domanda. – dhams

+0

Hai controllato questo link http://stackoverflow.com/questions/4721449/enable-gps-programatically-like-tasker? – Jitendra

risposta

41

Provare a utilizzare questo codice. Ha funzionato per me su tutte le versioni.

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); 
    } 
} 
+0

Grazie mille ... –

+0

questo codice attiva il gps nel mio dispositivo e non funziona disattivando sto usando 4.0.4 ics –

+2

hi non funziona in samsung galaxy y. mette il ricevitore GPS acceso sempre in modalità, dopo averlo risolto dovrebbe essere in modalità sleep .. – Sandeep

Problemi correlati