簡體   English   中英

Android 2.2:以編程方式重新啟動設備

[英]Android 2.2: Reboot device programmatically

我想知道是否可以通過代碼重新啟動設備。 我試過了:

Intent i = new Intent(Intent.ACTION_REBOOT); 
i.putExtra("nowait", 1); 
i.putExtra("interval", 1); 
i.putExtra("window", 0); 
sendBroadcast(i);

並為REBOOT添加了權限,但仍然無法正常工作。

謝謝

這似乎對我有用:

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

仍然對於有根設備,但如果您想更安全(將process.waitFor()設置為條件,則在單獨的try-catch中,我們將進行適當的異常處理,重新啟動后在命令中添加“ now”,這對於某些設備是必需的,等等。 ),也可能是更簡潔的代碼,請看一下:

Process rebootProcess = null;
try
{
    rebootProcess = Runtime.getRuntime().exec("su -c reboot now");
}
catch (IOException e)
{
    // Handle I/O exception.
}

// We waitFor only if we've got the process.
if (rebootProcess != null)
{
    try
    {
        rebootProcess.waitFor();
    }
    catch (InterruptedException e)
    {
        // Now handle this exception.
    }
}

您可以使用PowerManager使其重新啟動(這不能保證它將重新啟動-操作系統可能會取消它): 鏈接
鏈接2

我正在使用Xamarin。 對我來說,解決方案是:

Java.Lang.Runtime.GetRuntime().Exec(new String[] { "/system/xbin/su", "-c", "reboot now" });

這是一個解決方案。 請記住,該設備必須植根。

try{
    Process p = Runtime.getRuntime().exec("su");
    OutputStream os = p.getOutputStream();                                       
    os.write("reboot\n\r".getBytes());
    os.flush();
}catch(IOException )

如果手機已經扎根,那么實際上非常簡單:

try {
    Runtime.getRuntime().exec("su");
    Runtime.getRuntime().exec("reboot");
} catch (IOException e) {
}               

第一個命令將要求超級用戶許可。 第二,將重啟手機。 清單文件中不需要額外的權限,因為實際的重新啟動是由執行的命令而不是應用程序處理的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM