You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter项目调用代码重启安卓设备失败,权限报错求助

Android REBOOT权限问题解决方案

不需要重启设备,你的问题核心是REBOOT权限的系统级限制:

  • Android的android.permission.REBOOT是系统专属权限,普通第三方应用即便在AndroidManifest.xml中声明,也无法获得该权限——这个权限仅授予系统应用或拥有Root权限的应用。
  • 你当前使用的PowerManager.reboot()方法必须依赖该系统权限,所以普通应用调用必然触发权限报错。

针对不同场景的可行解决方案:

  1. Root设备测试场景
    通过Root权限执行系统重启命令替代PowerManager API:
    private void restart() {
        try {
            Process process = Runtime.getRuntime().exec("su -c reboot");
            process.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
    
  2. 系统应用场景
    • 在AndroidManifest.xml的<manifest>标签中添加android:sharedUserId="android.uid.system"属性
    • 使用与目标系统ROM一致的签名对应用进行签名
      完成上述操作后,即可正常调用PowerManager.reboot()方法。
  3. 系统广播方式(仍需系统权限)
    发送重启广播给系统组件,同样需要应用具备系统身份:
    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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 10:03:14