monkey测试关机/重启问题分析(三)

文章详细分析了Android系统中由Monkey测试引发的关机/重启问题,从StatusBarManagerService调用shutdown()开始,经过ShutdownThread的run()方法,再到PowerManagerService的lowLevelShutdown(),最后通过设置系统属性触发底层关机流程。在关机过程中涉及的关键步骤包括UI关闭、服务停止、电源管理等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前面几篇文章

Android 关机/重启原因分析
monkey测试关机/重启问题分析(一)
monkey测试关机/重启问题分析(二)
monkey测试关机/重启问题分析(三)

关机流程

关机流程可以参考以下其它人的文章https://blog.csdn.net/qq_29413633/article/details/120848128

触发流程可能不同,比如长按关机、点击dialog关机按钮关机等等。
1、上层关机流程

最终回走到ShutdowmThread。

本问接monkey测试关机/重启问题分析(二)中的流程继续往下,从systemui中调用mBarService.shutdown();最终走到frameworks/base/services/core/java/com/android/server/statusbar/StatusBarManagerService.java

1.1、StatusBarManagerService

    public void shutdown() {
        enforceStatusBarService();
        String reason = PowerManager.SHUTDOWN_USER_REQUESTED;
        ShutdownCheckPoints.recordCheckPoint(Binder.getCallingPid(), reason);//这里就是ShutdownCheckPoints log打印的地方,用来跟踪是哪个进程调用了关机
        final long identity = Binder.clearCallingIdentity();
        try {
            mNotificationDelegate.prepareForPossibleShutdown();
            // ShutdownThread displays UI, so give it a UI context.
            mHandler.post(() ->
                    ShutdownThread.shutdown(getUiContext(), reason, false));//这里调用到ShutdownThread
        } finally {
            Binder.restoreCallingIdentity(identity);
        }
    }

1.2、ShutdowmThread.run()

    public void run() {
        TimingsTraceLog shutdownTimingLog = newTimingsLog();
        shutdownTimingLog.traceBegin("SystemServerShutdown");
        ...
        //关机之前关掉其它service等一系列操作。
        ...
        // Remaining work will be done by init, including vold shutdown
        rebootOrShutdown(mContext, mReboot, mReason);
    }

1.3、ShutdownThread.rebootOrShutdown()

    public static void rebootOrShutdown(final Context context, boolean reboot, String reason) {
        //add for sreen off earlier
        if (sInstance.mPowerManager != null) {
            Log.i(TAG, "rebootOrShutdown:goToSleep");
            sInstance.mPowerManager.goToSleep(SystemClock.uptimeMillis());
        }

        if (reboot) {
            Log.i(TAG, "Rebooting, reason: " + reason);
            PowerManagerService.lowLevelReboot(reason);//重启
            Log.e(TAG, "Reboot failed, will attempt shutdown instead");
            reason = null;
        } else if (SHUTDOWN_VIBRATE_MS > 0 && context != null) {
            // vibrate before shutting down
            Vibrator vibrator = new SystemVibrator(context);
            try {
                vibrator.vibrate(SHUTDOWN_VIBRATE_MS, VIBRATION_ATTRIBUTES);
            } catch (Exception e) {
                // Failure to vibrate shouldn't interrupt shutdown.  Just log it.
                Log.w(TAG, "Failed to vibrate during shutdown.", e);
            }

            // vibrator is asynchronous so we need to wait to avoid shutting down too soon.
            try {
                Thread.sleep(SHUTDOWN_VIBRATE_MS);
            } catch (InterruptedException unused) {
            }
        }
        // Shutdown power
        Log.i(TAG, "Performing low-level shutdown...");
        PowerManagerService.lowLevelShutdown(reason);//关机
    }

1.4、PowerManagerService.lowLevelShutdown(reason);

    public static void lowLevelShutdown(String reason) {
        if (reason == null) {
            reason = "";
        }
        String chargeTypeString = "";
        if(mChargeType) {
            chargeTypeString  = ",charging";
        }
        //通过设置系统属性关机
        SystemProperties.set("sys.powerctl", "shutdown," + reason + chargeTypeString);
    }

上层Java代码就到这里,关机和重启都是设置系统属性后,底层c代码监听节点进行真正的操作。

SystemProperties.set("sys.powerctl", "reboot," + reason);//重启
SystemProperties.set("sys.powerctl", "shutdown," + reason + chargeTypeString);//关机

2、底层关机流程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值