24

Is it possible to automatically power on the device once the charger is connected given that the device is initially turned off?

6 Answers 6

15

fastboot oem off-mode-charge 0 is the genuine method if your device supports. It's Google's recommended method but not all OEMs/vendors implement the command in bootloader. Or on some devices it's reset on next reboot. If off-mode-charge is disabled, bootloader won't pass androidboot.mode=charger commandline parameter to kernel when charger is inserted, so device boots normally.

Otherwise when ro.bootmode property is set to charger on boot, init doesn't continue the normal boot process. Instead limited number of services are started and charging animation is displayed. So you can instruct init to reboot the device whenever charger mode is detected. Create a new .rc file or edit any existing one:

# /system/etc/init/off_mode_charge.rc

on charger
    setprop sys.powerctl reboot,leaving-off-mode-charging

Or execute reboot binary:

on charger
    exec - -- /system/bin/reboot leaving-off-mode-charging

But if SELinux is enforcing, stock policy may not let init execute /system/bin/reboot. So use Magisk's context (or whatever rooting solution you use):

on charger
    exec u:r:magisk:s0 -- /system/bin/reboot

Don't forget to set permissions on *.rc file (chown 0.0, chmod 0644, chcon u:object_r:system_file:s0).

It's also possible to continue boot process instead of restarting the device by replacing class_start charger with trigger late-init in /init.rc file:

on charger
    #class_start charger
    trigger late-init

Or by setting property sys.boot_from_charger_mode:

on charger
    setprop sys.boot_from_charger_mode 1
  • This method should work on all devices irrespective of OEM as it doesn't depend on vendor-specific charging binaries like playlpm, battery_charging, chargeonlymode, zchgd, kpoc_charger and so on.
  • Also replacing binaries of important services like healthd - which take care of a lot of things related to battery, storage etc. - is not a good idea. In this case if the service runs both in charger and normal mode, device may get into bootloop.
  • On non-System-as-Root devices it's not necessary to modify /system partition (e.g. if you don't want to break dm-verity for OTA updates to work). Simply unpack boot.img and edit /init.rc file in ramdisk.
  • Though unnecessary, it's also possible to execute an init.d script from .rc file. For reference see How to run an executable on boot? and How to power off when charger is removed?.

RELATED:

6
  • 1
    @JeffLuyet thanks for the path correction. You can ask a new question if something isn't properly addressed in existing questions/answers. I just saw your proposed edit by-chance. No notification is sent. // I don't know exactly why it didn't work for you. I have been playing with init's .rc file at least since Android 7. And it always worked. Usually SELinux is the most problematic part. You should have seen at kernel logs (dmesg) right after first failed attempt. init logs its events to kernel log. That's how the problem can be identified. Commented Feb 22, 2020 at 19:06
  • @IrfanLatif Thank you for your post here. I have the following issue, do I have to be root for this? I get gta3xlwifi:/etc/init $ cat > off-mode-charge.rc /system/bin/sh: can't create off-mode-charge.rc: Read-only file system
    – omega1
    Commented Jun 15, 2020 at 10:12
  • @omega1 definitely you need to be root. Or at least need to have custom recovery to modify boot.img or /system from there. None of the methods to automatically power on when charger is connected can work without root, except fastboot oem off-mode-charge (if supported on your device). Commented Jun 15, 2020 at 10:19
  • 1
    @IrfanLatif Thanks for confirming. It does not support fastboot oem off-mode-charge and I have failed miserably at rooting the device (Samsung SM-T510), I've followed every guide I can find and either get stuck with errors or fail at different parts of the steps (I must be missing something). I managed to load into TWRP, could I modify boot.img from there? I might look that up and see if I can do it that way. Who'd have thought it would be so hard!?
    – omega1
    Commented Jun 15, 2020 at 10:44
  • 1
    Thanks @IrfanLatif, your answer should be marked as best answer. By following this, I could successfully make my Xiaomi phone boot when charging. (I created file of /system/etc/init/off_mode_charge.rc which contains "on charger setprop sys.boot_from_charger_mode 1")
    – Loc Huynh
    Commented May 22 at 13:53
12

The battery charging graphic is displayed by /system/bin/playlpm file on Samsung devices.

If you are root, you can edit this file to:

#!/system/bin/sh
/system/bin/reboot

And be sure to add those permissions to the file:

chmod 0755 /system/bin/playlpm
chown root.shell /system/bin/playlpm

Your device will now boot when plugged

3
  • Hi, I have a Cyanogenmod device , do you know how can I do this on cyanogenmod ? Thanks
    – m0j1
    Commented Apr 20, 2016 at 2:34
  • This only works on Samsung devices. This may be useful for you
    – IET_DEMO
    Commented Apr 20, 2016 at 13:56
  • 1
    If you're trying to do this, make sure you don't have carriage returns in your playlpm file or it won't work. Commented Oct 4, 2016 at 12:38
5

So, there are many ways to do so. The most proper one, as always supported by Google (and more and more by others, e.g. nvidia from nougat) is this fastboot command

fastboot oem off-mode-charge 0

Otherwise, you can either hijack the charging binary (not guaranteed to be possible on all devices, but just requires root) or patch your ramdisk (theoretically universal, but will require an unlocked bootloader).

For the first solution, you'll have to find somewhere in your system partition (usually the bin folder) where the program in charge of the battery animation and all resides. Some common names:

  • Motorola: charge_only_mode
  • Mediatek: kpoc_charger or ipod
  • Htc: chargemon or zchgd
  • Samsung: playlpm or lpm
  • Sony: chargemon or battery_charging
  • Most(?) AOSP-based roms: healthd

Once found, you can just replace it with a script such as that above by IET_DEMO.

Touching the kernel is instead something I don't feel like explaining and recommending if you don't know what you are doing.

Just for the records then, I'd just like to underline that offline charging exists because the boot process is fairly energy intensive, and especially on older phones without even fast charging the power draw from the system could be higher than that on the plug.

3

Just got this working for ZTE Force (Boost Force, Sprint Force, etc).

I used IET_DEMO's answer, but replaced the file located at /system/bin/battery_charging with this code:

#!/system/bin/sh
/system/bin/reboot
2
  • Hi, I have a Cyanogenmod device , do you know how can I do this on cyanogenmod ? Thanks
    – m0j1
    Commented Apr 20, 2016 at 2:34
  • You'll need these commands: adb shell 'su -c mount -o rw,remount /system' and adb shell su -c 'chmod 755 /system/bin/battery_charging' and adb shell su -c 'chown root.shell /system/bin/battery_charging'
    – Jeff Luyet
    Commented Sep 22, 2023 at 15:55
2

For some older Samsung devices, this can be accomplished through NoMoarPowah!:

NoMoarPowah! can automatically reboot into Android when charging is done. Either when fully charged, or when the battery level reaches 15% and Android has enough juice to run.

You need root and you probably don't want to do it unless you really know what you're doing, since I would expect this to modify important system files.


It looks like this has been removed from the Play Store, but you can probably find the APK hosted elsewhere. (I don't have a trusted link offhand.)

0
0

Kernel is loaded at a later stage in the boot-up process, so any modifications to it would not have any effect for when the device is powered off.

This functionality really depends on each device's hardware, most likely in the bootloader (more knowledgeable editors feel free to correct me.) For example, my Motorola Atrix 4G phone begins booting up when I connect a charger, while my Samsung Galaxy Tab does not - it displays the "Battery Charging" graphic and requires being powered on manually.

2
  • 2
    so what brings up the Battery charging graphic on the screen ?!...now the bootloader is called when you hit on the power button manually, but what piece of code displays the battery charging graph on the screen ?! Commented Mar 4, 2012 at 9:19
  • 1
    In powered off state device's behavior on hardware events like power button and charger obviously depends on lower level code including SoC/PMIC firmware and bootloaders. But I haven't seen an Android device on which bootloader handles charging entirely itself without involving OS (not sure if there were in 2012). Bootloader hands over charge to Android kernel and even init is started. The only difference is that /data (and possibly other filesystems) are not mounted and a limited set of services is started as compared to normal userspace. Commented Feb 19, 2020 at 22:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .