的Android关闭GPS

问题描述:

我可以打开/关闭GPS在应用程序?由于它耗尽了太多的电池,我可以关闭一段时间吗?或者有这更好的解决办法.....的Android关闭GPS

尝试......

locationManager.removeUpdates(myLocationListener); locationManager = null; 

,不能打开/关闭它自己。然而,您可以提示用户关闭/打开:How to programmatically enable GPS in Android Cupcake

我认为作为一种安全措施,android系统不会允许您打开和关闭用户的gps(因为我认为是android 2.0) 。你可以停止获取更新,并且应该停止使用gps(除非其他应用程序使用它),或者要求用户将其关闭。

可以停止监听在你的应用GPS的事件(如果您的应用程序使用GPS)。例如,使用LocationManager.removeUpdates,例如 。

如果你想“关闭”在操作系统级别的GPS,我认为这是不可能的编程方式。

你,(用面包也许)要求用户要做到这一点,开始本次活动

if(!LocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) 
{ 
    Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
    startActivity(myIntent); 
} 
+0

这是什么会做什么? – user533844

+0

这应该启动用户可以禁用GPS的设置屏幕。 – Reno

+0

哦确定.....好主意.... – user533844

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