Android关机方法

Android系统关机接口总结如下,看下实现流程

一.framework接口关机
二.adb reboot -p
三.echo "o" > /proc/sysrq-trigger

一.framework接口关机

如低电量会自动关机,走如下流程

frameworks/base/services/java/com/android/server/power/PowerManagerService.java(http://aospxref.com/android-4.4.4_r2.0.1/xref/frameworks/base/services/java/com/android/server/power/PowerManagerService.java)

/**
     * Low-level function turn the device off immediately, without trying
     * to be clean.  Most people should use {@link ShutdownThread} for a clean shutdown.
     */
    public static void lowLevelShutdown() {
        SystemProperties.set("sys.powerctl", "shutdown");
    }

    /**
     * Low-level function to reboot the device. On success, this function
     * doesn't return. If more than 5 seconds passes from the time,
     * a reboot is requested, this method returns.
     *
     * @param reason code to pass to the kernel (e.g. "recovery"), or null.
     */
    public static void lowLevelReboot(String reason) {
        if (reason == null) {
            reason = "";
        }
        SystemProperties.set("sys.powerctl", "reboot," + reason);
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

system/core/rootdir/init.rc(http://aospxref.com/android-4.4.4_r2.0.1/xref/system/core/rootdir/init.rc#404

on property:sys.powerctl=*
            powerctl ${sys.powerctl}

system/core/init/keywords.h(http://aospxref.com/android-4.4.4_r2.0.1/xref/system/core/init/keywords.h)

KEYWORD(powerctl,    COMMAND, 1, do_powerctl)

system/core/init/builtins.c(http://aospxref.com/android-4.4.4_r2.0.1/xref/system/core/init/builtins.c

int do_powerctl(int nargs, char **args)
{
    char command[PROP_VALUE_MAX];
    int res;
    int len = 0;
    int cmd = 0;
    char *reboot_target;

    res = expand_props(command, args[1], sizeof(command));
    if (res) {
        ERROR("powerctl: cannot expand '%s'\n", args[1]);
        return -EINVAL;
    }

    if (strncmp(command, "shutdown", 8) == 0) {
        cmd = ANDROID_RB_POWEROFF;
        len = 8;
    } else if (strncmp(command, "reboot", 6) == 0) {
        cmd = ANDROID_RB_RESTART2;
        len = 6;
    } else {
        ERROR("powerctl: unrecognized command '%s'\n", command);
        return -EINVAL;
    }

    if (command[len] == ',') {
        reboot_target = &command[len + 1];
    } else if (co
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值