如何在Android手机中以编程方式禁用GPS?

时间:2014-01-27 13:27:13

标签: android gps

我需要在某些事件后以编程方式禁用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();
        }
    }

2 个答案:

答案 0 :(得分: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上这样做,因为这是不可能的。虽然有一些黑客,比如你问题中的那些,曾经工作过,但这些安全漏洞已被修复。

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