0

我想在 Activity OnCreated 时打开 GPS。我正在使用此代码,但它不起作用。我收到此错误消息:

java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE from pid=3818, uid=10084 Min SDK API 17 Max SDK API 23

我习惯了这个代码:

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

我试试这个代码。但我得到同样的问题。

Intent intent=new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);`
4

1 回答 1

0

由于安全/隐私问题,不允许以编程方式启用/禁用 GPS。

如果您指的是线程,则此错误已得到修复。

您可以使用它打开 GPS 设置页面并让用户手动启用或禁用 GPS,

startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
于 2016-07-22T09:04:01.923 回答