android关机流程后

在用户确认关机后,关机流程会走到ShutdownThread中.

ShutdownThread包含过具体关机和关机动画处理.
ShutdownThread.java

  public static void shutdown(final Context context, String reason, boolean confirm) {
        mReboot = false;
        mRebootSafeMode = false;
        mReason = reason;
        shutdownInner(context, confirm);
    }
    public static void reboot(final Context context, String reason, boolean confirm) {
        mReboot = true;
        mRebootSafeMode = false;
        mRebootHasProgressBar = false;
        mReason = reason;
        shutdownInner(context, confirm);
    }

上面分别是关机和重启的方法,差别在于mReboot的值.

再次确认关机或重启的提示框

再次确认关机或重启的提示框


    private static void shutdownInner(final Context context, boolean confirm) {
.....
        if (confirm) {
            ....
            sConfirmDialog = new AlertDialog.Builder(context)
                    .setTitle(TinnoFeature.FEATURE_REBOOT_DIALOG 
                            ? resourceIdTitle 
                            : (mRebootSafeMode
                                    ? com.android.internal.R.string.reboot_safemode_title
                                    : com.android.internal.R.string.power_off))
                    .setMessage(TinnoFeature.FEATURE_REBOOT_DIALOG ? resourceIdMsg : resourceId)
                    .setPositiveButton(com.android.internal.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            beginShutdownSequence(context);
                        }
                    })
                    .setNegativeButton(com.android.internal.R.string.no, null)
                    .create();
            closer.dialog = sConfirmDialog;
            sConfirmDialog.setOnDismissListener(closer);
            sConfirmDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
            sConfirmDialog.show();
            } else {
                beginShutdownSequence(context);
            }
        }
        ....

这是是显示关机提示框,让用户确认是否关机,如果方法传入的参数confirm是false就不会有这个提示框.

关机动画的加载

上面代码在用户确认关机或重启之后,会走到beginShutdownSequence方法

ShutdownThread.java

    private static void beginShutdownSequence(Context context) {
        synchronized (sIsStartedGuard) {
            if (sIsStarted) {
                Log.d(TAG, "Shutdown sequence already running, returning.");
                return;
            }
            sIsStarted = true;
        }

        // SPRD:add for shutdownanim
        if (shutdownAnim.hasShutdownAnimation() &&
                !(mReason != null && mReason.startsWith(PowerManager.REBOOT_RECOVERY_UPDATE))) {
            shutdownAnim.playShutdownAnimation();
        } else {
            sInstance.mProgressDialog = showShutdownDialog(context);
        }
        sInstance.mContext = context;
        sInstance.mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);

        // make sure we never fall asleep again
        sInstance.mCpuWakeLock = null;
        try {
            sInstance.mCpuWakeLock = sInstance.mPowerManager.newWakeLock(
                    PowerManager.PARTIAL_WAKE_LOCK, TAG + "-cpu");
            sInstance.mCpuWakeLock.setReferenceCounted(false);
            sInstance.mCpuWakeLock.acquire();
        } catch (SecurityException e) {
            Log.w(TAG, "No permission to acquire wake lock", e);
            sInstance.mCpuWakeLock = null;
        }

        // also make sure the screen stays on for better user experience
        sInstance.mScreenWakeLock = null;
        if (sInstance.mPowerManager.isScreenOn()) {
            try {
                sInstance.mScreenWakeLock = sInstance.mPowerManager.newWakeLock(
                        PowerManager.FULL_WAKE_LOCK, TAG + "-screen");
                sInstance.mScreenWakeLock.setReferenceCounted(false);
                sInstance.mScreenWakeLock.acquire();
            } catch (SecurityException e) {
                Log.w(TAG, "No permission to acquire wake lock", e);
                sInstance.mScreenWakeLock = null;
            }
        }

        if (SecurityLog.isLoggingEnabled()) {
            SecurityLog.writeEvent(SecurityLog.TAG_OS_SHUTDOWN);
        }

        // start the thread that initiates shutdown
        if(sIsPowerOffAlarm){
            Looper.prepare();
            sInstance.mHandler = new Handler();
            Log.d(TAG,">>> before start <<<");
            sInstance.start();
            Looper.loop();
        }else{
        sInstance.mHandler = new Handler() {
        };
        sInstance.start();
        }
    }

PowerManagerService真正关机

PowerManagerService.lowLevelReboot(reason);
PowerManagerService.lowLevelShutdown(reason);

    public static void lowLevelShutdown(String reason) {
        if (reason == null) {
            reason = "";
        }
        SystemProperties.set("sys.powerctl", "shutdown," + reason);
    }

设置SystemProperties.set(“sys.powerctl”, “reboot,” + reason);或
SystemProperties.set(“sys.powerctl”, “shutdown,” + reason);通知 通知init 执行。

然后到源码System目录init.cpp ,reboot.cpp处理这些关机操作。

发布了67 篇原创文章 · 获赞 42 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/w1764662543/article/details/87607367