1. 程式人生 > >android 開機預設進入指定Launcher

android 開機預設進入指定Launcher

這裡總結下我研究這個需求,想出的兩種解決方案。
第一種方法最簡單暴力只要修改apk的AndroidManifest直接上原始碼

<activity
            android:name="com.android.launcher3.Launcher"
            android:launchMode="singleTask"
            android:clearTaskOnLaunch="true"
            android:stateNotNeeded="true"
            android:theme="@style/Theme"
android:windowSoftInputMode="adjustPan" android:screenOrientation="nosensor">
<intent-filter android:priority="2"> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.HOME"
/>
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.MONKEY"/> </intent-filter> </activity>

這裡就加了一句android:priority=”2”,這樣在開機和按HOME鍵時候系統intent判斷到category.HOME屬性後如果有多個此屬性apk,則會進入ResolverActivity讓使用者選擇。當你定義了此優先順序它其他未定義的都預設為0,所以優先進入了你的activity。

第二種方法需要修改framework原始碼來強制進入你的launcher
首先ActivityManagerService.java中

boolean startHomeActivityLocked(int userId) {
        if (mHeadless) {
            // Added because none of the other calls to ensureBootCompleted seem to fire
            // when running headless.
            ensureBootCompleted();
            return false;
        }

        if (mFactoryTest == SystemServer.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) {
            //add wwd start
            PackageManager pm = mContext.getPackageManager();
            Intent newintent = new Intent(Intent.ACTION_MAIN);
            newintent.addCategory(Intent.CATEGORY_HOME);

            List<ResolveInfo> resolveInfoList = pm.queryIntentActivities(newintent, 0);
            //判斷帶有Intent.CATEGORY_HOME標籤的所有activity中如果有你指定的activity則替換
            if(resolveInfoList != null){
                int size = resolveInfoList.size();
                for(int i = 0; i < size; i++){
                    ResolveInfo rInfo = resolveInfoList.get(i);
                    if(rInfo.activityInfo.name.equals("com.android.launcher3.Launcher")){
                        aInfo = rInfo.activityInfo;
                    }
                }
            }
            //add wwd stop
            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);
                //這裡啟動你已替換過的activity
                mStackSupervisor.startHomeActivity(intent, aInfo);
            }
        }

        return true;
    }

這樣就把開機homeactivity替換成你想要啟動的luncher了。
下面在修改按home鍵強制退回的launcher
PhoneWindowManager.java

public void init(Context context, IWindowManager windowManager,
            WindowManagerFuncs windowManagerFuncs) {
            ......
            mHomeIntent =  new Intent(Intent.ACTION_MAIN, null);
        mHomeIntent.addCategory(Intent.CATEGORY_HOME);
        //add wwd start
        ComponentName mHomecom = new ComponentName("com.android.launcher3", "com.android.launcher3.Launcher"); 
        mHomeIntent.setComponent(mHomecom);
        //add wwd stop
        mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            ......
}

這裡新增以上兩句讓home強制跳轉指定launcher就好了。