2013-03-15 13 views
27

Merhaba, bir hırsızlık karşıtı uygulama oluşturuyorum ve telefonumu sms aracılığıyla konumlandırıyorum ve 2.3'e kadar mükemmel çalışıyor. Fakat 4.0'da gps'yi programatik olarak açıp kapatamıyorum, kod aracılığıyla gps'yi açmanın başka bir yolu var.Android'de programatik olarak GPS'i devre dışı bırakmayı nasıl etkinleştirebilirim?

+0

http://stackoverflow.com/questions/4721449/enable-gps-programatically-like-tasker sorunuzu kod burada – Dilip

+2

geçmiş pic bakmak gerekir. – dhams

+0

Bu bağlantıyı kontrol ettiniz http://stackoverflow.com/questions/4721449/enable-gps-programatically-like-tasker? – Jitendra

cevap

41

Bu kodu kullanmayı deneyin. Tüm versiyonlarda benim için çalıştı.

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

Çok teşekkürler ... –

+0

Bu kod, cihazımda gps'i açıyor ve ben de çalışmıyor çünkü 4.0.4 ics kullanıyorum –

+2

hi samsung galaxy y'de çalışmıyor. Gps alıcısı her zaman açık moddayken, uyku modundan çıktıktan sonra uyku moduna geçmelidir .. – Sandeep

İlgili konular