2014-01-27 103 views
1

我需要一些編程的事件後關閉GPS,我使用下面的方法,但我不能夠做到這一點......如何以編程方式在android手機中禁用GPS?

private void turnGPSOff(){ 
     String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
     if(provider.contains("gps")){ 
      final Intent intent = new Intent(); 
      intent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
      intent.addCategory(Intent.CATEGORY_ALTERNATIVE); 
      intent.setData(Uri.parse("3")); 
      sendBroadcast(intent); 
      Toast.makeText(getBaseContext(), "GPS Turned off..!", Toast.LENGTH_LONG).show(); 
     } 
    } 
+0

轉到我的回答:[http://stackoverflow.com/questions/20740365/programmatically-turn-on-gps-in-android/20740483#20740483](http://stackoverflow.com/questions/20740365 /以編程方式打開gps-in-android/20740483#20740483) –

+0

@MD ya,但始終保持GPS消耗電池電量,所以我想這樣做.. –

回答

1

嘗試此關閉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); 
    } 
} 

但你只能在2.3以下的版本上做到這一點。你不能在4.x上做,因爲這是不可能的,因爲明顯的隱私原因。儘管曾經有一些黑客像您的問題那樣,曾經有效,但這些安全漏洞已經得到修復。

+0

所以我們怎麼才能在android中實現它4.x? – gumuruh

+0

你不能,但你可以給用戶選擇打開/關閉GPS ...意圖gpsOptionsIntent =新意圖( android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(gpsOptionsIntent); – Umar

+1

你能給我提供任何參考,我可以在4.X版本中閱讀這個修復程序嗎? – Shad

0
LocationManager L = (LocationManager) getSystemService(LOCATION_SERVICE); 

if (!L.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
    Intent I = new Intent( 
       android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
     startActivity(I); 
} 
相關問題