Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

android - Restarting a device programmatically

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...