2015-10-07 78 views
6

在我的android應用程序中,我想重新啓動我的android設備按鈕單擊。 但它不工作。
到目前爲止,我已經完成了這項工作。以編程方式重新啓動android設備

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

而且我把下面的許可清單文件。

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

當我點擊圖像按鈕重新啓動,我得到以下異常。

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 

我的代碼出了什麼問題?請幫助我。
有關信息,我正在測試android棒棒糖(如果它很重要)。
在此先感謝。

+1

看到這個答案:http://stackoverflow.com/questions/4966486/reboot-the-phone-on-a-button-click – wblaschko

回答

3

試試這個...

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

添加權限重啓

+0

我已經試過這一點。但是同樣的例外。 – Suniel

+0

作品在Android 2.2的...... –

+0

這是依賴於Android版本?其實我在測試的Android 5.0.1 – Suniel

7

你需要的是不相關的重啓方法,允許作爲你的方法需要有根的電話(與su)。要重新啓動手機,請按照您的要求獲得許可,但請致電PowerManager#reboot

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
pm.reboot(null); 
+1

我得到這個異常: java.lang.SecurityException異常:無論用戶10695也不是當前進程android.permission.REBOOT。 – Suniel

+0

確保您擁有清單文件中的權限。 – MByD

+0

我在清單文件中擁有權限。 – Suniel

2

長期鬥爭後,我發現工作液。

如果您的系統使用的串行端口,然後執行以下命令,

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

如果使用普通端口則excure以下命令

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

差只有/bin/xbin

所以可以編寫代碼,如果第一個命令拋出異常,然後執行第二個。

+0

我看不出有任何區別.... – nrofis

+1

你能再次檢查,我糾正錯字 –

5

在API 24,如果你的應用是設備所有者的應用程序,你可以撥打:devicePolicyManager.reboot(yourAdminComponent)

查看文檔here

0

可以使用傳說中的LibSuperUser庫。

寫這個簡單的代碼:

Shell.SU.run("reboot"); 
0

你正在做<permission ... />。 您需要做的<USES-permission ... />

上限不是必需的,只是爲了強調它。

相關問題