Flutter项目调用代码重启安卓设备失败,权限报错求助
Android REBOOT权限问题解决方案
不需要重启设备,你的问题核心是REBOOT权限的系统级限制:
- Android的
android.permission.REBOOT是系统专属权限,普通第三方应用即便在AndroidManifest.xml中声明,也无法获得该权限——这个权限仅授予系统应用或拥有Root权限的应用。 - 你当前使用的
PowerManager.reboot()方法必须依赖该系统权限,所以普通应用调用必然触发权限报错。
针对不同场景的可行解决方案:
- Root设备测试场景
通过Root权限执行系统重启命令替代PowerManagerAPI:private void restart() { try { Process process = Runtime.getRuntime().exec("su -c reboot"); process.waitFor(); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } - 系统应用场景
- 在
AndroidManifest.xml的<manifest>标签中添加android:sharedUserId="android.uid.system"属性 - 使用与目标系统ROM一致的签名对应用进行签名
完成上述操作后,即可正常调用PowerManager.reboot()方法。
- 在
- 系统广播方式(仍需系统权限)
发送重启广播给系统组件,同样需要应用具备系统身份:private void restart() { Intent rebootIntent = new Intent(Intent.ACTION_REBOOT); rebootIntent.putExtra("nowait", 1); rebootIntent.putExtra("interval", 1); rebootIntent.putExtra("window", 0); sendBroadcast(rebootIntent); }
内容的提问来源于stack exchange,提问作者Manoj
相关产品推荐
相关产品推荐

