以编程方式重新启动Android Things

我想使用此代码重新启动运行Android Things的 RPI3

public static void Reboot() { try { Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "reboot"}); proc.waitFor(); } catch (Exception ex) { ex.printStackTrace(); } } 

我收到以下错误:

java.io.IOException:无法运行程序“su”:error = 13,权限被拒绝

我向清单添加了所有权限

                

我错过了什么吗?

更新:这可能不适用于较新的操作系统版本

DP 4中的 /system/bin/reboot二进制文件,因此在之前的所有开发预览中都具有世界可执行权限,即以下产量

 adb shell ls -l /system/bin | grep reboot -rwxr-xr-x 1 root shell ... reboot 

也就是说,可以为任何用户(也就是Android中的 app进程)执行二进制文件,而无需抓取su 。 只需在Java中执行即可

重启

 Runtime.getRuntime().exec("reboot"); 

或者用于断电

 Runtime.getRuntime().exec("reboot -p"); 

AndroidManifest.xml 无需成功即可成功运行二进制文件。

您现在可以使用以下命令重新启动:

https://developer.android.com/things/reference/com/google/android/things/devicemanagement/DeviceManager.html

  public class SomeActivity extends Activity { void doReboot() { DeviceManager.getInstance().reboot(); } void doFactoryReset() { boolean wipeExternalCard = true; DeviceManager.getInstance().factoryReset(wipeExternalCard); } } 

您需要com.google.android.things.permission.REBOOT权限

在未来的开发人员预览版中,在Android Things上运行的应用程序可以访问受系统保护的function(例如由PowerManager.reboot() API提供的function)。 您不能(也不应该)尝试通过su以root用户身份运行您的应用程序进程。

另请注意,Android Things的开发人员映像构建为userdebug ,这意味着您可以通过在尝试访问shell之前使用adb root命令重新启动ADB守护程序来从shell访问root。 这为您提供了在开发过程中可能需要的任何root访问权限,而不会影响设备安全性并允许应用程序进程以root身份运行。

您需要root访问权限。

 public static void runCmd(String cmd) { DataOutputStream os; try { Process process = Runtime.getRuntime().exec("su"); os = new DataOutputStream(process.getOutputStream()); os.writeBytes(cmd + "\n"); os.writeBytes("exit\n"); os.flush(); os.close(); } catch (IOException e) { e.printStackTrace(); } } 

然后,您可以运行任何需要root访问权限的命令,如下所示: runCmd("reboot");