Android:如何判断系统是第一次开机

Android:如何判断系统是第一次开机_第1张图片
三体

在系统的首次启动过程中,可能要做一些事情,但后面的启动不做。有什么方法判断手机是第一次启动呢?PMS里面有接口介绍,简单看下。

/frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java
PackageManagerService用下面的接口可以获取是否是第一次启动。

    @Override
    public boolean isFirstBoot() {
        return mFirstBoot;
    }

看看mFirstBoot的赋值,是在PackageManagerService构造函数进行赋值。

public PackageManagerService(Context context, Installer installer,
            boolean factoryTest, boolean onlyCore) {
 .....
 mFirstBoot = !mSettings.readLPw(sUserManager.getUsers(false));
 ......
 }

mSettings类是
/frameworks/base/services/core/java/com/android/server/pm/Settings.java

final Settings mSettings;
mSettings = new Settings(mPackages);

    final ArrayMap mPackages =
            new ArrayMap();

看看这个类的readLPw函数罗

boolean readLPw(@NonNull List users) {
        FileInputStream str = null;
        //第一次启动data/system/packages-backup.xml肯定不存在,所以不会进入这个条件
        if (mBackupSettingsFilename.exists()) {
            try {
                str = new FileInputStream(mBackupSettingsFilename);
                mReadMessages.append("Reading from backup settings file\n");
                PackageManagerService.reportSettingsProblem(Log.INFO,
                        "Need to read from backup settings file");
                if (mSettingsFilename.exists()) {
                    // If both the backup and settings file exist, we
                    // ignore the settings since it might have been
                    // corrupted.
                    Slog.w(PackageManagerService.TAG, "Cleaning up settings file "
                            + mSettingsFilename);
                    mSettingsFilename.delete();
                }
            } catch (java.io.IOException e) {
                // We'll try for the normal settings file.
            }
        }
       ..................
       .................
        try {
            if (str == null) {
                //第一次启动data/system/packages.xml肯定不存在,所以这里会进入,返回false,进而确定是第一次启动
                if (!mSettingsFilename.exists()) {
                    mReadMessages.append("No settings file found\n");
                    PackageManagerService.reportSettingsProblem(Log.INFO,
                            "No settings file; creating initial state");
                    // It's enough to just touch version details to create them
                    // with default values
                    findOrCreateVersion(StorageManager.UUID_PRIVATE_INTERNAL);
                    findOrCreateVersion(StorageManager.UUID_PRIMARY_PHYSICAL);
                    return false;
                }
      .................
     ................
}

所以,判断第一次启动其实是通过判断data/system/packages.xml是否存在来实现的,如果文件不存在,则认为是系统第一次启动。写完啦!!

你可能感兴趣的:(Android:如何判断系统是第一次开机)