Restart android device programmatically

2019-01-18 06:22发布

In my android application, I want to restart my android device on button click. But its not working.
I have done this so far.

ImageButton restartmob = (ImageButton) this.findViewById(R.id.restartmob);
    restartmob.setOnClickListener(new ImageButton.OnClickListener() {
        @Override
        public void onClick(View v) {
            Process proc = null;            
            try {
               //proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" });
                 proc = Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot now"});
                proc.waitFor();
            } catch (Exception ex) {
                Log.i(TAG, "Could not reboot", ex);
            }
        }
    });

Also I put the following permission in the manifest file.

<permission android:name="android.permission.REBOOT"/>

When I clicked the image button to restart, I got the following exception.

java.io.IOException: Error running exec(). Command: [/system/bin/su, -c, reboot now] 
Working Directory: null Environment: null   
at java.lang.ProcessManager.exec(ProcessManager.java:211)   
at java.lang.Runtime.exec(Runtime.java:173)
at java.lang.Runtime.exec(Runtime.java:128)
at com.android.onlinepayment.activity.SystemSubMenuActivity$1.onClick(SystemSubMenuActivity.java:49)
at android.view.View.performClick(View.java:5184)   
at android.view.View$PerformClick.run(View.java:20910)  
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)    
at java.lang.reflect.Method.invoke(Native Method)   
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Caused by: java.io.IOException: No such file or directory   
at java.lang.ProcessManager.exec(Native Method)
at java.lang.ProcessManager.exec(ProcessManager.java:209)
... 13 more

What's wrong with my code? Please help me on this.
For the info, I am testing on android Lollipop (if it matters).
Thanks in Advance.

7条回答
贼婆χ
2楼-- · 2019-01-18 06:43

After long struggle i found working solution.

If your system is used serial port then execute below command,

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

if use normal port then excure below command

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

difference is only /bin and /xbin

so can code like if first command throw exception then execute second.

查看更多
相关推荐>>
3楼-- · 2019-01-18 06:46

The permission you required is not related to your reboot method, as your method requires a rooted phone (with su). To reboot the phone, require the permission as you did, but call PowerManager#reboot.

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
pm.reboot(null);
查看更多
对你真心纯属浪费
4楼-- · 2019-01-18 06:47

You can use the legendary LibSuperUser library.

Write this simple code:

Shell.SU.run("reboot");
查看更多
来,给爷笑一个
5楼-- · 2019-01-18 06:48

Try this...

try {
      Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" });
      proc.waitFor();
         } catch (Exception ex) {
                Log.i(TAG, "Could not reboot", ex);
      }

Add permission reboot

查看更多
smile是对你的礼貌
6楼-- · 2019-01-18 06:48

You cannot do a reboot from an ordinary SDK application. Only applications signed with the system firmware signing key can do this. Copied from this answer,

Programmatically switching off Android phone

You need the system key to sign your app. See this post for details;

How to compile Android Application with system permissions

查看更多
Evening l夕情丶
7楼-- · 2019-01-18 06:51

You are doing <permission ... />. You need to do <USES-permission ... />

The caps are not needed, it was just to emphasise it.

查看更多
登录 后发表回答