Android10 替换Launcher3

Android10 替换Launcher3,第1张

需求:使用自己的应用作为launcher应用

1.想要替换桌面launcher,首先你自己的应用中要有home属性


    
    
    
    

2.路径: /frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

//新增方法

private void setDefaultLauncher(){
        // 替换应用的包名
    String defaultlauncherpckname = "com.xxx.xxx.xxxx";

    PackageManager mPm = mContext.getPackageManager();
    Intent mIntent = new Intent();
    mIntent.setAction(Intent.ACTION_MAIN);
    mIntent.addCategory(Intent.CATEGORY_HOME);
    List mList = mPm.queryIntentActivities(mIntent, 0);
    ComponentName[] mHomeComponentSet = new ComponentName[mList.size()];
    ComponentName newHome = null;
    
    for (int i = 0; i < mList.size(); i++) {
         ActivityInfo info = mList.get(i).activityInfo;
         ComponentName componentName = new ComponentName(info.packageName, info.name);
         mHomeComponentSet[i] = componentName;
         if (info.packageName.equals(defaultlauncherpckname)) {
             newHome = componentName;
         }
    }
        
    if (newHome != null) {
        IntentFilter mHomeFilter = new IntentFilter();
        mHomeFilter.addAction(Intent.ACTION_MAIN);
        mHomeFilter.addCategory(Intent.CATEGORY_HOME);
        mHomeFilter.addCategory(Intent.CATEGORY_DEFAULT);

        mPm.replacePreferredActivity(mHomeFilter, IntentFilter.MATCH_CATEGORY_EMPTY, mHomeComponentSet, newHome);
    }
}

//在此方法中调用

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;
    }
    // set default launcher 此处调用 设置默认apk为LAUNCHER
    setDefaultLauncher();

    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;
}
 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/992215.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存