2013-03-15 21 views
27

Witam, tworzę aplikację antywłamaniową i znajduję mój telefon za pomocą sms i działa idealnie do 2.3. Ale w wersji 4.0 nie można programowo włączyć lub wyłączyć GPS, jest jakikolwiek inny sposób włączenia GPS przez kod.Jak mogę włączyć GPS programowo w systemie Android?

+0

http://stackoverflow.com/questions/4721449/enable-gps-programatically-like-tasker mają wyglądać tutaj – Dilip

+2

przeszłości pic kod w swoim pytaniu. – dhams

+0

Czy sprawdziłeś ten link http://stackoverflow.com/questions/4721449/enable-gps-programatically-like-tasker? – Jitendra

Odpowiedz

41

Spróbuj użyć tego kodu. Sprawdził się we wszystkich wersjach.

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

Bardzo dziękuję ... –

+0

ten kod włącza GPS w moim urządzeniu i nie działa na wyłączenie Używam 4.0.4 ics –

+2

hi nie działa w Samsung Galaxy Y. po ustawieniu, odbiornik GPS jest włączony, po naprawie powinien być w trybie uśpienia. – Sandeep

Powiązane problemy