Android設定預設Launcher

來源:互聯網
上載者:User

標籤:android   launcher   預設   源碼   

當系統中有多個Launcher案頭時,可通過以下方式設定預設的Launcher:

方法一:

偷懶的話,將不想要的Launcher,如Launcher3的AndroidManifest.xml檔案中的HOME屬性去掉,只保留自己想要的Launcher即可,前提是我們有其他Launcher的源碼。弊端是去掉Home屬性後再也無法進入。

方法二:

1.編輯:
frameworks/base/services/java/com/android/server/pm/PackageManagerService.java
檔案中的
public void systemReady() {}函數,在最後添加以下程式碼片段:

// TChip ZJ Add Below for Default Launcher        if (isFirstBoot()) {            // 修改為需要設定的 package name            String examplePackageName = "com.tchip.carlauncher";            // 修改為需要設定的 launcher activity name            String exampleActivityName = "com.tchip.carlauncher.ui.activity.MainActivity";            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);            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);                    set[i] = new ComponentName(                            resolveInfo.activityInfo.packageName,                            resolveInfo.activityInfo.name);                    if (defaultLauncher.getClassName().equals(                            resolveInfo.activityInfo.name)) {                        defaultMatch = resolveInfo.match;                    }                }                IntentFilter filter = new IntentFilter();                filter.addAction(Intent.ACTION_MAIN);                filter.addCategory(Intent.CATEGORY_HOME);                filter.addCategory(Intent.CATEGORY_DEFAULT);                addPreferredActivity2(filter, defaultMatch, set,                        defaultLauncher);            }        }        // TChip ZJ Add Above for Default Launcher

2.添加以下函數:

/**     * TChip ZJ Add for Default Launcher     */    public void addPreferredActivity2(IntentFilter filter, int match,            ComponentName[] set, ComponentName activity) {        Log.d("debug_default", "addPreferredActivity2 is called.");        // writer        synchronized (mPackages) {            Slog.i(TAG, "Adding preferred activity " + activity + ":");            filter.dump(new LogPrinter(Log.INFO, TAG), " ");            mSettings.editPreferredActivitiesLPw(0).addFilter(                    new PreferredActivity(filter, match, set, activity, true));            scheduleWriteSettingsLocked();        }    }

3.修改PackageManagerService.java的函數findPreferredActivity:
(1)對removeMatches進行條件判斷,在launcher時不remove:

if (!(intent.getAction() != null                            && intent.getAction().equals(intent.ACTION_MAIN)                            && intent.getCategories() != null && intent.getCategories()                            .contains(intent.CATEGORY_HOME))) {                        Slog.d(TAG, "launcher");} else {                        if (removeMatches) {                            pir.removeFilter(pa);                            if (DEBUG_PREFERRED) {                                Slog.v(TAG, "Removing match " + pa.mPref.mComponent);                            }                            break;                        }}

(2)

    if (always && !pa.mPref.sameSet(query, priority)) {            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;        }

上面這段修改為下面的內容:

if (always && !pa.mPref.sameSet(query, priority)) {            if (!(intent.getAction() != null                    && intent.getAction().equals(intent.ACTION_MAIN)                    && intent.getCategories() != null && intent.getCategories()                    .contains(intent.CATEGORY_HOME))) { // MTK ADD                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;            }        }        Slog.i(TAG,                "Result set not change, not drop preferred activity for special intent: "                        + intent);// MTK ADD        // Yay! Either the set matched or we‘re looking for the last chosen

著作權聲明:本文原創,轉載請註明出處:http://blog.csdn.net/zhoumushui

Android設定預設Launcher

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.