Powering off Android Things
Asked Answered
E

3

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?

Electro answered 6/1, 2017 at 10:41 Comment(3)
Good question. Nothing in the documentation, the "embedded"/IoT OS should handle sudden lack of power gracefully though.Newborn
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 problemWsan
@Wsan 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 possibilityElectro
S
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.

Sidewalk answered 6/1, 2017 at 20:56 Comment(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 dirElectro
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.Kev
@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.Smallsword
F
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)

Farrago answered 6/1, 2017 at 21:3 Comment(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!Electro
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 :-)Farrago
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...Electro
A
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.

Anosmia answered 26/5, 2017 at 20:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.