android 设置默认launcher

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/michael_yt/article/details/78112262

本博客基于 Android 7.0,只作为沟通学习使用。

前言

平时使用android手机的时候我们可能会遇到下面的情况,比如:我们有多个浏览器,当我们没有设置哪一个为默认的浏览器并点击了一行网址时,就会弹出一个系统选择框,让用户选择一个浏览来打开这个网址,并且会让用户选择只使用一次还是将你选择的浏览器设为默认的浏览。那么问题就来了,如果我不想出现这个提示框,我就想第一次开机时,就自动帮我设置好默认的浏览器可以吗?答案是可以的。

对于浏览器、sms、邮箱等应用我们可以在 frameworks/base/core/res/res/values/config.xml 这个配置文件里面直接修改,类似于 <string name="default_browser" translatable="false">com.xxx.yyy.browser</string> 这种直接配置我们要设置成默认应用的包名就可以了,系统启动的时候会去读取这个配置文件里面的信息,但是我们在文件里面没有找到设置默认的 launcher target,那么我们怎么设置默认的launcher应用呢?

整体介绍

通过查看launcher的启动流程我们可以发现, SystemServer 调用 ActivityManagerService.systemReady 然后 systemReady 会调用 startHomeActivityLocked 去获取当前系统存在的launcher应用,最后通过 ActivityStarter.startHomeActivityLocked 去启动当前系统中launcher的主界面(如果有多个就会弹出对话框让用户选择)。

主要代码如下:

获取当前的launcher的信息:

    boolean startHomeActivityLocked(int userId, String reason) {
        if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
                && mTopAction == null) {
            // We are running in factory test mode, but unable to find
            // the factory test app, so just sit around displaying the
            // error message and don't try to start anything.
            return false;
        }
        Intent intent = getHomeIntent();
        ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
        if (aInfo != null) {
            intent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));
            // Don't do this if the home app is currently being
            // instrumented.
            aInfo = new ActivityInfo(aInfo);
            aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
            ProcessRecord app = getProcessRecordLocked(aInfo.processName,
                    aInfo.applicationInfo.uid, true);
            if (app == null || app.instrumentationClass == null) {
                intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
                mActivityStarter.startHomeActivityLocked(intent, aInfo, reason);
            }
        } else {
            Slog.wtf(TAG, "No home screen found for " + intent, new Throwable());
        }

        return true;
    }

在ActivityStarter里面启动launcher的主界面:

    void startHomeActivityLocked(Intent intent, ActivityInfo aInfo, String reason) {
        mSupervisor.moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
        startActivityLocked(null /*caller*/, intent, null /*ephemeralIntent*/,
                null /*resolvedType*/, aInfo, null /*rInfo*/, null /*voiceSession*/,
                null /*voiceInteractor*/, null /*resultTo*/, null /*resultWho*/,
                0 /*requestCode*/, 0 /*callingPid*/, 0 /*callingUid*/, null /*callingPackage*/,
                0 /*realCallingPid*/, 0 /*realCallingUid*/, 0 /*startFlags*/, null /*options*/,
                false /*ignoreTargetSecurity*/, false /*componentSpecified*/, null /*outActivity*/,
                null /*container*/, null /*inTask*/);
        if (mSupervisor.inResumeTopActivity) {
            // If we are in resume section already, home activity will be initialized, but not
            // resumed (to avoid recursive resume) and will stay that way until something pokes it
            // again. We need to schedule another resume.
            mSupervisor.scheduleResumeTopActivities();
        }
    }

解决办法

我们已经知道系统会在 startHomeActivityLocked 这个方法里面去拿到当前系统的launcher 的信息,然后会通过ActivityStarter去启动界面,如果有多个就会显示选择框,如果有默认的那么就启动默认的launcher。所以,我们就解决办法就是 在 startHomeActivityLocked 方法里面就直接把某个launcher设置成默认的launcher,那么当开机完成后进入launcher就不会显示选择框了,具体代码如下:

修改systemReady.startHomeActivityLocked 如下:

boolean startHomeActivityLocked(int userId, String reason) {
        if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
                && mTopAction == null) {
            // We are running in factory test mode, but unable to find
            // the factory test app, so just sit around displaying the
            // error message and don't try to start anything.
            return false;
        }

        final PackageManager mPm = mContext.getPackageManager();

        Intent homeIntent=new Intent();

        homeIntent.addCategory(Intent.CATEGORY_HOME);

        homeIntent.setAction(Intent.ACTION_MAIN);
        homeIntent.addCategory(Intent.CATEGORY_DEFAULT);
        ResolveInfo info = mPm.resolveActivity(homeIntent, PackageManager.MATCH_DEFAULT_ONLY);
        if("android".equals(info.activityInfo.packageName)){

            ComponentName DefaultLauncher=new ComponentName("com.xxx.xxx.launcher",
                    "com.xxx.xxx.launcher.activities.MainLauncherActivity");
            ArrayList<ResolveInfo> homeActivities = new ArrayList<ResolveInfo>();
            ComponentName currentDefaultHome = mPm.getHomeActivities(homeActivities);

            ComponentName[]mHomeComponentSet = new ComponentName[homeActivities.size()];
            for (int i = 0; i < homeActivities.size(); i++) {
                final ResolveInfo candidate = homeActivities.get(i);
                Log.d(TAG,"homeActivitie: candidate = "+candidate);
                final ActivityInfo activityInfo= candidate.activityInfo;
                ComponentName activityName = new ComponentName(activityInfo.packageName, activityInfo.name);
                mHomeComponentSet[i] = activityName;
            }
            IntentFilter mHomeFilter = new IntentFilter(Intent.ACTION_MAIN);
            mHomeFilter.addCategory(Intent.CATEGORY_HOME);
            mHomeFilter.addCategory(Intent.CATEGORY_DEFAULT);
            List<ComponentName>Activities=new ArrayList();
            mPm.replacePreferredActivity(mHomeFilter, IntentFilter.MATCH_CATEGORY_EMPTY,mHomeComponentSet, DefaultLauncher);

        }


        Intent intent = getHomeIntent();
        ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
        if (aInfo != null) {
            intent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));
            // Don't do this if the home app is currently being
            // instrumented.
            aInfo = new ActivityInfo(aInfo);
            aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
            ProcessRecord app = getProcessRecordLocked(aInfo.processName,
                    aInfo.applicationInfo.uid, true);
            if (app == null || app.instrumentationClass == null) {
                intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
                mActivityStarter.startHomeActivityLocked(intent, aInfo, reason);
            }
        } else {
            Slog.wtf(TAG, "No home screen found for " + intent, new Throwable());
        }

        return true;
    }

其它

在MTK online上面搜到了同样的问题,但是上面多了一步针对预置了GMS套件的修改方案,如下:

在 PackageManagerService.systemReady 的方法的末尾加上如下代码:

if(isFirstBoot()) { //如果是刷机后第一次开机就进入
            String examplePackageName = "com.xxx.xxx.launcher";
            String exampleActivityName = "com.xxx.xxx.launcher.activities.MainLauncherActivity";
            Intent intent=new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            final int callingUserId = UserHandle.getCallingUserId();

            List<ResolveInfo> resolveInfoList = queryIntentActivities(intent,null, PackageManager.GET_META_DATA,callingUserId).getList();
            if(resolveInfoList != null){
                int size = resolveInfoList.size();
                for(int j=0;j<size;){
                    final ResolveInfo r = resolveInfoList.get(j);
                    if(!r.activityInfo.packageName.equals(examplePackageName)) {
                        resolveInfoList.remove(j);
                        size -= 1;
                    } else {
                        j++;
                    }
                }
                ComponentName[] set = new ComponentName[size];
                ComponentName defaultLauncher=new ComponentName(examplePackageName, exampleActivityName);
                int defaultMatch=0;
                for(int i=0;i<size;i++){
                    final ResolveInfo resolveInfo = resolveInfoList.get(i);
                    Log.d(TAG,"resolveInfo = " + resolveInfo.toString());
                    set[i] = new ComponentName(resolveInfo.activityInfo.packageName,resolveInfo.activityInfo.name);
                    if(defaultLauncher.getClassName().equals(resolveInfo.activityInfo.name)){
                        defaultMatch = resolveInfo.match;
                    }
                }
                Log.d(TAG,"defaultMatch="+Integer.toHexString(defaultMatch));
                IntentFilter filter=new IntentFilter();
                filter.addAction(Intent.ACTION_MAIN);
                filter.addCategory(Intent.CATEGORY_HOME);
                filter.addCategory(Intent.CATEGORY_DEFAULT);

                addPreferredActivity2(filter, defaultMatch, set, defaultLauncher);
            }
        }
    public void addPreferredActivity2(IntentFilter filter, int match,ComponentName[] set, ComponentName activity) {
        synchronized (mPackages) {
            filter.dump(new LogPrinter(Log.INFO, TAG), "  ");
            mSettings.editPreferredActivitiesLPw(0).addFilter(new PreferredActivity(filter, match, set, activity, true));
            scheduleWriteSettingsLocked();
        }
    }

然后再修改 PackageManagerService.findPreferredActivity 中的部分代码如下:

                            /* if (removeMatches) {
                                pir.removeFilter(pa);
                                changed = true;
                                if (DEBUG_PREFERRED) {
                                    Slog.v(TAG, "Removing match " + pa.mPref.mComponent);
                                }
                                break;
                            }

                            // Okay we found a previously set preferred or last chosen app.
                            // If the result set is different from when this
                            // was created, we need to clear it and re-ask the
                            // user their preference, if we're looking for an "always" type entry.
                            if (always && !pa.mPref.sameSet(query)) {
                                Slog.i(TAG, "Result set changed, dropping preferred activity for "
                                        + intent + " type " + resolvedType);
                                if (DEBUG_PREFERRED) {
                                    Slog.v(TAG, "Removing preferred activity since set changed "
                                            + pa.mPref.mComponent);
                                }
                                pir.removeFilter(pa);
                                // Re-add the filter as a "last chosen" entry (!always)
                                PreferredActivity lastChosen = new PreferredActivity(
                                        pa, pa.mPref.mMatch, null, pa.mPref.mComponent, false);
                                pir.addFilter(lastChosen);
                                changed = true;
                                return null;
                            } */

                            if(!(intent.getAction() != null && intent.getAction().equals(intent.ACTION_MAIN) && intent.getCategories()!=null &&
intent.getCategories().contains(intent.CATEGORY_HOME))) {
                                Log.d(TAG,"Home");
                            }else {

                                if (removeMatches) {
                                    pir.removeFilter(pa);
                                    if (DEBUG_PREFERRED) {
                                        Slog.v(TAG, "Removing match " + pa.mPref.mComponent);
                                    }
                                    break;
                                }

                            }

                            // Okay we found a previously set preferred or last chosen app.
                            // If the result set is different from when this
                            // was created, we need to clear it and re-ask the
                            // user their preference, if we're looking for an "always" type entry.
                            if (always && !pa.mPref.sameSet(query)) {
                                if(!(intent.getAction() != null && intent.getAction().equals(intent.ACTION_MAIN) && intent.getCategories()!=null &&
                                        intent.getCategories().contains(intent.CATEGORY_HOME))) {
                                    Slog.i(TAG, "Result set changed, dropping preferred activity for "
                                            + intent + " type " + resolvedType);
                                    if (DEBUG_PREFERRED) {
                                        Slog.v(TAG, "Removing preferred activity since set changed "
                                                + pa.mPref.mComponent);
                                    }
                                    pir.removeFilter(pa);
                                    // Re-add the filter as a "last chosen" entry (!always)
                                    PreferredActivity lastChosen = new PreferredActivity(
                                            pa, pa.mPref.mMatch, null, pa.mPref.mComponent, false);
                                    pir.addFilter(lastChosen);
                                    mSettings.writePackageRestrictionsLPr(userId);
                                    return null;
                                }
                            }

上面的做法可能是防止安装GMS套件的时候Google Now Launcher(GoogleHome.apk)这个应用会自动设置为默认的,所以在PMS中,如果发现是刷机后第一次开机,那么我们就会强制把默认的launcher设置成我们想要的哪一个apk。

猜你喜欢

转载自blog.csdn.net/michael_yt/article/details/78112262