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?
/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.
You can now do a reboot using:
https://developer.android.com/things/reference/com/google/android/things/devicemanagement/DeviceManager.html
Example
public class SomeActivity extends Activity {
void doReboot() {
DeviceManager.getInstance().reboot();
}
void doFactoryReset() {
boolean wipeExternalCard = true;
DeviceManager.getInstance().factoryReset(wipeExternalCard);
}
}
You need the com.google.android.things.permission.REBOOT
permission
Access to system-protected features (such as those provided by the PowerManager.reboot()
API) will be available to apps running on Android Things in a future developer preview release. You cannot (and should not) attempt to run your app process as the root user via su
.
As a side note, the developer images for Android Things are built as userdebug
, which means you can access root from the shell by simply rebooting the ADB daemon with the adb root
command before you attempt to access the shell. This gives you any root access you may need during development without compromising the device security and allowing app processes to run as root.
You need a root access.
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();
}
}
Then you can run any commands that require root access like this: runCmd("reboot");