Reboot programmatically Android Things

Daniele picture Daniele · May 23, 2017 · Viewed 7.4k times · Source

I want to use this code in order to reboot my RPI3 running Android Things:

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

I get the following error:

java.io.IOException: Cannot run program "su": error=13, Permission denied

I add to the manifest all the permission

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.SET_TIME_ZONE"/>
<uses-permission android:name="android.permission.SHUTDOWN"/>

Am I missing something?

Answer

Onik picture Onik · May 23, 2017

/system/bin/reboot binary in DP 4, so as in all the previous dev previews, has world-executable permission, i.e. the following yields

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

That said, it is yet possible to execute the binary for any user (a.k.a app process in Android) without a need to grab su. Just execute in Java for

rebooting

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

or for powering off

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

No permission's needed in AndroidManifest.xml to run the binary successfully.

Caution: in case of security model changes in newer OS versions this approach may not work.