-1

我正在尝试在我的应用程序中关闭并重新启动。我用谷歌搜索并在 stackoverflow 上找到了一些已回答的问题,但都说设备必须植根,这在我的情况下是不可能的。这些命令也是特定于设备的。是否有任何选项可以关闭或重新启动 android 设备以编程
方式尝试以下操作

String command="/system/bin/reboot";

        try { // Run Script

            proc = runtime.exec("su");
            osw = new OutputStreamWriter(proc.getOutputStream());
                                osw.write(command);
                    osw.flush();
            osw.close();

        } catch (IOException ex) {}

Runtime.getRuntime().exec(new String[]{"su","-c","reboot now"});
4

1 回答 1

0

The devices i've got now are rooted so i can't test it but any how you need to sign your application with device keys or make it a system app.

This is an example for app that works fine for me :

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
   pm.reboot(null);
}

And add to manifest :

uses-permission android:name="android.permission.REBOOT"/>

Hope it helps

于 2015-08-30T13:03:30.947 回答