我正在尝试以编程方式将飞机模式设置为打开。
Settings.System.putInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
Intent aeroPlaneIntent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
aeroPlaneIntent.putExtra("state", true);
context.sendBroadcast(aeroPlaneIntent);设置Global.AIRPLANE_MODE_ON失败,就像我检查它返回0(关闭)的状态一样
尝试广播它的意图时会引发以下异常:
03-12 07:01:18.747: E/AndroidRuntime(1579): java.lang.RuntimeException: Unable to start receiver com.example.toggleaeroplanemode.AeroplaneModeReceiver: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.AIRPLANE_MODE from pid=1579, uid=10050
03-12 07:01:18.747: E/AndroidRuntime(1579): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2431)
03-12 07:01:18.747: E/AndroidRuntime(1579): at android.app.ActivityThread.access$1500(ActivityThread.java:141)
03-12 07:01:18.747: E/AndroidRuntime(1579): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
03-12 07:01:18.747: E/AndroidRuntime(1579): at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 07:01:18.747: E/AndroidRuntime(1579): at android.os.Looper.loop(Looper.java:137)
03-12 07:01:18.747: E/AndroidRuntime(1579): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-12 07:01:18.747: E/AndroidRuntime(1579): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 07:01:18.747: E/AndroidRuntime(1579): at java.lang.reflect.Method.invoke(Method.java:525)
03-12 07:01:18.747: E/AndroidRuntime(1579): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-12 07:01:18.747: E/AndroidRuntime(1579): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-12 07:01:18.747: E/AndroidRuntime(1579): at dalvik.system.NativeStart.main(Native Method)
03-12 07:01:18.747: E/AndroidRuntime(1579): Caused by: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.AIRPLANE_MODE from pid=1579, uid=10050
03-12 07:01:18.747: E/AndroidRuntime(1579): at android.os.Parcel.readException(Parcel.java:1431)
03-12 07:01:18.747: E/AndroidRuntime(1579): at android.os.Parcel.readException(Parcel.java:1385)
03-12 07:01:18.747: E/AndroidRuntime(1579): at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:2224)
03-12 07:01:18.747: E/AndroidRuntime(1579): at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1046)
03-12 07:01:18.747: E/AndroidRuntime(1579): at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:344)
03-12 07:01:18.747: E/AndroidRuntime(1579): at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:344)
03-12 07:01:18.747: E/AndroidRuntime(1579): at com.example.toggleaeroplanemode.AeroplaneModeReceiver.onReceive(AeroplaneModeReceiver.java:31)
03-12 07:01:18.747: E/AndroidRuntime(1579): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2424)
03-12 07:01:18.747: E/AndroidRuntime(1579): ... 10 more有没有办法把飞行模式设为开/关?请帮帮忙
发布于 2014-03-12 19:24:50
关于一些小信息:
请先使用此权限..
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
更多信息:只需检查此conversation
发布于 2018-03-09 13:41:09
以下内容在Android API 23 & 26模拟器上进行了测试和验证;
所需的清单权限如下;
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" tools:ignore="ProtectedPermissions"/>代码如下:
boolean turnOnOffAirplaneMode(boolean isTurnOn) {
boolean result = true;
try {
Settings.Global.putInt(context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, isTurnOn ? 1 : 0);
// The below old code is now not necessary @ API Level 23, 26 ...
//Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
//context.sendBroadcast(intent);
} catch (Exception e) {
result = false;
}
return result;
}APK必须使用相关的平台密钥签名才能工作。在Emulator测试中,APK是用Google Sign Keys签名的。密钥取决于设备的OEM。
https://stackoverflow.com/questions/22349928
复制相似问题