我想在Activity OnCreated时打开GPS。我正在使用此代码,但它不起作用。我得到这个错误信息:Android自动打开GPS
java.lang.SecurityException异常:权限拒绝:不允许从PID = 3818,UID发送广播android.location.GPS_ENABLED_CHANGE = 10084 敏SDK API 17最大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);`
你不能从你的代码..安全问题打开GPS和也违反谷歌的政策。你可以做的最好的是添加运行时权限来访问棉花糖和设备上的GPS – tk1505