2014-05-25 66 views
0

我要啓用和禁用GPS。我想要谷歌地圖應用程序,當運行應用程序時,應用程序詢問我啓用我的位置,當我的應用程序進入背景(暫停狀態)GPS禁用,我的意思是我沒有看到屏幕頂部的GPS圖標,當再次應用程序進入前臺,我的位置再次啓用。我沒有使用LocationOverlay。我在onPause()方法中編寫locationManager.removeUpdates(locationListener);,但是當我的應用轉到背景時,我看到gps並啓用並嘗試檢測我的位置!禁用GPS位置

我寫的啓用位置,但我不知道如何禁用GPS,然後隱藏GPS圖標!

請幫我

謝謝你的建議。

回答

1

Disable GPS programmatically on Android

private void turnGPSOff(){ 
     String provider = Settings.Secure.getString(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")); 
      sendBroadcast(poke); 
     } 
    } 

要刪除的圖標和關閉GPS:

locationManager.removeUpdates(myLocationListener); 
locationManager = null; 
+0

感謝您的回答。我寫沒有removerUpdates()和應用程序去背景的turnGPSOff()方法。 GPS關閉。 removeUpdates()和turnGPSOff()有什麼區別? – user3209380

+0

和另一個問題。我希望當第二個答案禁用GPS發送鏈接,我的GPS禁用,但是,當我再次去應用程序,在onResume(),我如何檢測GPS禁用後,我啓用此功能,如果GPS關閉,我打開GPS!我用這個代碼檢測gps的開啓和關閉:(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) – user3209380

0

嘗試那些:

private void turnGPSOn() 
{ 
String provider = Settings.Secure.getString(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")); 
sendBroadcast(poke); 
} 
} 

private void turnGPSOff() 
{ 
String provider = Settings.Secure.getString(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")); 
sendBroadcast(poke); 
} 
} 

請注意,你需要設置適當的應用程序的權限。

+0

關閉並使用相同的代碼,所以我們可以只使用一個開關函數來開啓和關閉 – Xcihnegn

+0

yupp,只是想顯示它是如何總體工作... –