Android 低电量关机原理 详解

首先电量过低时,healthd会触发batteryService类中的shutdownIfNoPowerLocked方法:
private void shutdownIfNoPowerLocked() {
    // shut down gracefully if our battery is critically low and we are not powered.
    // wait until the system has booted before attempting to display the shutdown dialog.
    if (mBatteryProps.batteryLevel == 0 && !isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (mActivityManagerInternal.isSystemReady()) {
                    Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
                    intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivityAsUser(intent, UserHandle.CURRENT);
                }
            }
        });
    }
}

该方法会跳转到ShutdownActivity中:
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    mReboot = Intent.ACTION_REBOOT.equals(intent.getAction());
    mConfirm = intent.getBooleanExtra(Intent.EXTRA_KEY_CONFIRM, false);
    mUserRequested = intent.getBooleanExtra(Intent.EXTRA_USER_REQUESTED_SHUTDOWN, false);
    Slog.i(TAG, "onCreate(): confirm=" + mConfirm);

    Thread thr = new Thread("ShutdownActivity") {
        @Override
        public void run() {
            IPowerManager pm = IPowerManager.Stub.asInterface(
                    ServiceManager.getService(Context.POWER_SERVICE));
            try {
            	//这里会执行管理或者重启操作
                if (mReboot) {
                    pm.reboot(mConfirm, null, false);
                } else {
                    pm.shutdown(mConfirm,
                                mUserRequested ? PowerManager.SHUTDOWN_USER_REQUESTED : null,
                                false);
                }
            } catch (RemoteException e) {
            }
        }
    };
    thr.start();
    finish();
    // Wait for us to tell the power manager to shutdown.
    try {
        thr.join();
    } catch (InterruptedException e) {
    }
}

参考文档:
低电量管理-framework层-详解

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值