36

Usually, to power down an Android device, you do this via the power button of course.

You can also do adb shell and reboot -p.

But in Android Things, I don't see a way to shut down the device. If it is no problem, I'd love to just cut the power of my Raspberry Pi for this, but is that acceptable? Could it corrupt the SD-card?

3
  • Good question. Nothing in the documentation, the "embedded"/IoT OS should handle sudden lack of power gracefully though. Jan 6, 2017 at 10:56
  • I'd expect Android Things to be designed in a way that unplugging the power is fine. I've done that a few times with my Dev Kit and does not seem to be a problem
    – shalafi
    Jan 6, 2017 at 11:25
  • @shalafi I hope so. I've done this plenty of times with Linux on the Pi during development, but I made read-only SD-card for production where the customer wanted this possibility
    – Boy
    Jan 6, 2017 at 12:52

3 Answers 3

25

Android (and by extension, Android Things) should have no problem with a sudden loss of power. The core operating system is housed in read-only partitions on the file system, so there is no risk of corrupting the OS from a failed in-flight write.

Also, reboot -p should still work if you wanted to use that in testing or development. Going even farther with it, you could connect a Gpio with an InputDriver that emits KEYCODE_POWER to add your own power button back to the system if you felt you needed it.

3
  • Thanks, I think I will go sort of this way. I need to do it remotely, probably using a websocket and shutdown on a specific command. I think unplugging the power should also be ok as it is read only. As long as I don't let the app write something to it's files dir
    – Boy
    Jan 9, 2017 at 8:17
  • 1
    Just experiencing sudden loss power, and sdcard being corrupted as result. Plug it into ubuntu, can't detect the sdcard. Plug into my Android phone, it detect the sdcard, and show warning sdcard need to be formatted. Format from Android phone, plug into ubuntu, won't detect again. Plug into Windows, it detect, then format it from Windows. Thankfully, works again. So beware.
    – adadion
    Jun 22, 2017 at 7:03
  • 1
    @Devunwired At the moment KEYCODE_POWER is not particularly useful for devices without a display and touchscreen. A short press on KEYCODE_POWER turns off the screen as it attempts to put the device in sleep mode, and a long press displays an onscreen menu that offers options to "Power off" and "Restart" that must be clicked on. Dec 12, 2017 at 16:57
24

I like Dave's Answer, just wanted to add two things:

You could shut down the Android Things device programatically a number of ways but each has a caveat attached to it, discussed here: Turn off device programmatically

To power off an AndroidThings device like you said you can do it via ADB:

 adb shell reboot -p

(-p is short for --poweroff)

3
  • I need it to be shut down remotely, the device runs headless. I don't want a customer te need to do 'adb shell reboot -p' :) But the link you provided gives me various options, thanks!
    – Boy
    Jan 9, 2017 at 8:16
  • You could use Firebase cloud messaging to notify your app, then do one of the programmatic options. Yeah you're right about ADB an customers :-)
    – Blundell
    Jan 9, 2017 at 8:17
  • This device will not be connected to the internet in my use case, but it is a nice option for some other use case I'm thinking of...
    – Boy
    Jan 9, 2017 at 8:18
11

... and programmatically

for powering off

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

and rebooting

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

Explanation: the reboot binary is shipped in Android Things image with the world-executable permission, i.e. rwxr-xr-x, which makes it possible to be executable from within any app process. In other words, an app process does not need to gain su in contrast to most stock Android phones/tablets, so is no extra permission needed in AndroidManifest.xml.

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.