Search code examples
androidandroid-4.2-jelly-bean

Android ICS/JB, Reboot to Safe Mode Programmatically?


I am making a Utility app for my galaxy nexus. I want to reboot my tablet in safe mode.

I tried to look in PowerManager

PowerManager p = (PowerManager) getSystemService(POWER_SERVICE);
p.reboot(reason);

It seems this will not reboot the device in safe mode. Is it possible to reboot the device programmatically? How?


Solution

  • Basically there are two known ways to enter Safe Mode:

    1. Android detects a problem with a newly installed app and force-closes it while entering into Safe Mode.

    2. A combination of key presses at power application;

    I doubt there's yet another way of doing it. If there was, most recoveries and power menus of Custom ROMS would have included that.

    The string passed to reboot() is a kernel param, and would have effect only if device's kernel has that option. You can try some options here.

    UPDATE:

    Safe Mode is toggle is inside PackageManagerService of Android's system server ("package" service):

    public void enterSafeMode() {
        enforceSystemOrRoot("Only the system can request entering safe mode");
    
        if (!mSystemReady) {
            mSafeMode = true;
        }
    }
    

    and here are some points about using it from any APP :

    1. Process executing this code must be System or have Root previleges
    2. This is an internal service and off-limits to any outside code. Though, some system classes indeed get implementation stubs (IPackageManager) of this service.
    3. The mode change can only be useful when system is yet to be ready.
    4. Let's suppose your app does turn on safe mode some how, due to safe mode being enabled, it won't be around to turn it off. Unless its a system app, built into ROM.