Turn Off Device Programmatically

Turn off device programmatically

In my case, I do not think it is possible to shut the device down how I would like to.

The closest that I managed to get to my target was using:

Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
i.putExtra("android.intent.extra.KEY_CONFIRM", true);
startActivity(i);

That brings up a dialog to turn the device off. This is the perfect solution, but in my case, using it causes the device to crash. It may be that my device is somewhat special, and other devices will not have these restrictions.

In any case, I hope that my testing will help others in their quest.

Programmatically switching off Android phone

You could possibly use the PowerManager to make it reboot (this does not guarantee that it'll reboot - OS may cancel it):

http://developer.android.com/reference/android/os/PowerManager.html#reboot(java.lang.String)

It requires the REBOOT permission:

http://developer.android.com/reference/android/Manifest.permission.html#REBOOT

Can you also check your logcat when trying to enable/disable keyguard, and post what's there?

Schedule Turn on/off android device programmatically on specific times

this is simply not possible, once you turn it off you lose control over the device.

How to shutdown an Android mobile programmatically?

You could possibly use the PowerManager to make it reboot (this does not guarantee that it'll reboot - OS may cancel it):

http://developer.android.com/reference/android/os/PowerManager.html#reboot(java.lang.String)

It requires the REBOOT permission:

http://developer.android.com/reference/android/Manifest.permission.html#REBOOT

Can you also check your logcat when trying to enable/disable keyguard, and post what's there?

You cannot do this from an ordinary SDK application. Only applications signed with the system firmware signing key can do this

Android : Turn on a device programmatically

The mechanism for doing this relies on replacing the battery animation script, which is run while the device is turned off but plugged in, typically displaying an icon of the charging battery. The name of the script varies from device to device, but it is generally located in the /system/bin directory. Samsung devices generally call the script playlpm, and other names for the script that I've seen include ipod, lpm, and battery_charging. This will not necessarily work on every device, because this is well outside of the standard Android framework -- some devices might not have an equivalent script, or they might implement it in a different way.

This could be characterized as an "exploit" in that it requires root and works at the Linux level rather than the Android framework level, but there is currently no alternative for implementing this behavior.

The general mechanism for making this change is described here: https://android.stackexchange.com/questions/20021/automatically-power-on-android-when-the-charger-is-connected. Of course it's a good idea to back up the previous battery animation script before you do any of this.

The following script has worked for me on multiple devices (several Samsung devices and the Verizon Ellipsis 7). Basically, it checks to see if the phone is plugged into AC power and has enough charge. If so, it boots up. If not, it waits for N seconds and tries again. As a side effect, the original battery animation script won't run, and you won't ever see the pretty charging animation.

#!/system/bin/sh                                                                               

# battery threshold before boot-up (in percent)
bthresh=10

# time to sleep between checks (in seconds)
sleeptime=600

# file that contains current battery level as integer between 0 and 100
cfi=/sys/class/power_supply/battery/capacity
# file that contains 1 if we're plugged in to AC, 0 if not
acfi=/sys/class/power_supply/battery/subsystem/ac/online

# if either file doesn't exist, just do normal sleep+boot
[ ! -f $cfi ] && sleep $sleeptime && /system/bin/reboot
[ ! -f $acfi ] && sleep $sleeptime && /system/bin/reboot

# populate capacity and AC variables
c=`cat $cfi`
ac=`cat $acfi`

# stop loop if we're not plugged into AC
until [ "$ac" -eq 0 ]
do
# if capacity above threshold, boot up
if [ "$c" -gt "$bthresh" ]; then
/system/bin/reboot
fi

# wait some time before next check
sleep $sleeptime

# update capacity and AC variables
c=`cat $cfi`
ac=`cat $acfi`
done


Related Topics



Leave a reply



Submit