简体   繁体   English

Permission Denial:不允许发送广播 android.intent.action.AIRPLANE_MODE

[英]Permission Denial: not allowed to send broadcast android.intent.action.AIRPLANE_MODE

I am trying to programmatically set the aeroplane mode on.我正在尝试以编程方式设置飞行模式。

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

Setting the Global.AIRPLANE_MODE_ON is failing as if I check the status it returns 0 (OFF)设置 Global.AIRPLANE_MODE_ON 失败,就好像我检查它返回 0 (OFF) 的状态

Trying to broadcast the intent it raises the following exception:尝试广播它会引发以下异常的意图:

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

Is there a way to set the airplane mode ON/OFF?有没有办法设置飞行模式开/关? Please help请帮忙

For little info:小信息:

Use this Permission first..首先使用此权限..

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

further info: Just check this conversation更多信息:只需查看此对话

The below was tested and verified on Android API 23 & 26 Emulators;以下内容已在 Android API 23 和 26 模拟器上进行了测试和验证;

The manifest permission needed is as follows;所需的清单权限如下;

<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" tools:ignore="ProtectedPermissions"/>

The code is as follows;代码如下;

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

The APK should be signed with the relevant platform key for above to work. APK 应使用相关平台密钥进行签名,以便上述工作。 In the Emulator test, the APK was signed with Google Sign Keys.在模拟器测试中,APK 使用 Google Sign Keys 进行签名。 The keys depend on the OEM of the device.密钥取决于设备的 OEM。

$ adb shell
$ su
# settings put global airplane_mode_on 0
# am broadcast -a android.intent.action.AIRPLANE_MODE --ez state false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SecurityException:权限被拒绝:不允许发送广播android.intent.action.BATTERY_CHANGED - SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.BATTERY_CHANGED 权限拒绝:不允许发送广播android.intent.action.HEADSET_PLUG - Permission Denial: not allowed to send broadcast android.intent.action.HEADSET_PLUG CSipSimple-java.lang.SecurityException:权限拒绝:不允许发送广播android.intent.action.Phone_State - CSipSimple - java.lang.SecurityException : Permission Denial: not allowed to send broadcast android.intent.action.Phone_State 应用程序因 Permission Denial 崩溃:不允许发送广播 android.intent.action.SCREEN_ON - App crashes with Permission Denial: not allowed to send broadcast android.intent.action.SCREEN_ON java.lang.SecurityException:权限拒绝:不允许在android 7(N OS)上发送广播android.intent.action.NEW_OUTGOING_CALL - java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.NEW_OUTGOING_CALL on android 7 (N OS) 权限拒绝:不允许在android中发送广播 - Permission Denial: not allowed to send broadcast in android 权限拒绝:不允许发送广播android - Permission Denial: not allowed to send broadcast android java.lang.SecurityException:权限拒绝:不允许仅在 KitKat 上发送广播 android.intent.action.MEDIA_MOUNTED - java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED on KitKat only 无需用户交互即可自动更新 APP ==&gt; Permission Denial: not allowed to send broadcast android.intent.action.PACKAGE_ADDED - Update APP automatically without user interaction facing issue==> Permission Denial: not allowed to send broadcast android.intent.action.PACKAGE_ADDED 拒绝权限:不允许发送广播android.bluetooth.device.action.FOUND - Permission Denial: not allowed to send broadcast android.bluetooth.device.action.FOUND
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM