anddroid9.0设置默认launcher

这篇博客深入解析了Android系统中如何设置默认启动器(launcher)以及在什么条件下启动主活动的过程。通过源码分析,我们可以看到在无法找到工厂测试应用时的处理逻辑,以及如何根据Intent匹配和启动主活动的相关细节。同时,还展示了如何清除已有的首选活动并添加新的首选活动到系统中。
摘要由CSDN通过智能技术生成
frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
    boolean startHomeActivityLocked(int userId, String reason) {
		 setDefaultLauncher();//调用设置默认launcher方法
        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.instr == null) {
                intent.setFlags(intent.getFlags() | FLAG_ACTIVITY_NEW_TASK);
                final int resolvedUserId = UserHandle.getUserId(aInfo.applicationInfo.uid);
                // For ANR debugging to verify if the user activity is the one that actually
                // launched.
                final String myReason = reason + ":" + userId + ":" + resolvedUserId;
                mActivityStartController.startHomeActivity(intent, aInfo, myReason);
            }
        } else {
            Slog.wtf(TAG, "No home screen found for " + intent, new Throwable());
        }

        return true;
    }
//设置默认launcher的方法
        private void setDefaultLauncher() {
            boolean mFirstLaunch = true;
            String packageName = "com.xxxxx.smartlauncher";//launcher包名
            String className = "Launcher";
            if ((packageName != null && packageName.trim().length() > 1) && (className != null && className.trim().length() > 0)) {
                if(mFirstLaunch){
                    IPackageManager pm = ActivityThread.getPackageManager();
                    ArrayList<IntentFilter> intentList = new ArrayList<IntentFilter>();
                    ArrayList<ComponentName> cnList = new ArrayList<ComponentName>();
                    mContext.getPackageManager().getPreferredActivities(intentList, cnList, null);
                    IntentFilter dhIF;
                    for(int i = 0; i < cnList.size(); i++)
                    {
                        dhIF = intentList.get(i);
                        if(dhIF.hasAction(Intent.ACTION_MAIN) &&
                                dhIF.hasCategory(Intent.CATEGORY_HOME))
                        {
                            mContext.getPackageManager().clearPackagePreferredActivities(cnList.get(i).getPackageName());
                        }
                    }
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_HOME);
                    List<ResolveInfo> list = new ArrayList<ResolveInfo>();
                    try {
                        list = pm.queryIntentActivities(intent,
                                intent.resolveTypeIfNeeded(mContext.getContentResolver()),
                                PackageManager.MATCH_DEFAULT_ONLY,UserHandle.getCallingUserId()).getList(); //add .getList()
                    }catch (RemoteException e) {
                        throw new RuntimeException("Package manager has died", e);
                    }
                    // get all components and the best match
                    IntentFilter filter = new IntentFilter();
                    filter.addAction(Intent.ACTION_MAIN);
                    filter.addCategory(Intent.CATEGORY_HOME);
                    filter.addCategory(Intent.CATEGORY_DEFAULT);
                    final int N = list.size();
                    Slog.d(TAG, "N:::::hyhyhyhy:::: = " + N);
                    ComponentName[] set = new ComponentName[N];
                    int bestMatch = 0;
                    for (int i = 0; i < N; i++)
                    {
                        ResolveInfo r = list.get(i);
                        set[i] = new ComponentName(r.activityInfo.packageName,
                                r.activityInfo.name);
                        Slog.d(TAG, "r.activityInfo.packageName:::::hyhyhyhy:::: = " + r.activityInfo.packageName);
                        Slog.d(TAG, "r.activityInfo.name:::::hyhyhyhy:::: = " + r.activityInfo.name);
                        if (r.match > bestMatch) bestMatch = r.match;
                    }
                    //<C9><E8><D6><C3>?<C8><CF>launcher
                    ComponentName launcher = new ComponentName(packageName, className);
                    try
                    {
                        Log.d("zy","pm.addPreferredActivity");
                        pm.addPreferredActivity(filter, bestMatch, set, launcher,UserHandle.getCallingUserId());
                    } catch (RemoteException e) {
                        throw new RuntimeException("Package manager has died", e);
                    }
                }
            }
        }
//end

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

那肯定是很多年以后!

你的鼓励就我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值