禁用GPS位置

时间:2014-05-25 05:56:28

标签: android gps

我想要启用和禁用GPS。我想要谷歌地图应用程序,当运行应用程序时,应用程序问我启用我的位置,当我的应用程序进入后台(暂停状态)gps禁用,我的意思是我没有看到屏幕顶部的gps图标,当再次应用程序转到前台,再次启用我的位置。我没有使用LocationOverlay。我在locationManager.removeUpdates(locationListener);方法中写了onPause(),但是当我的应用程序转到后台时,我会看到gps并启用并尝试检测我的位置!

我写了启用位置,但我不知道如何禁用gps然后隐藏gps图标!

请帮帮我

感谢您的建议。

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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);
}
}

请注意您需要设置正确的应用权限。

相关问题