android 开机默认进入指定Launcher

本文介绍两种定制Android设备启动界面的方法。第一种通过修改AndroidManifest.xml文件,设置特定Launcher的优先级,使系统在开机和按HOME键时优先显示。第二种涉及修改ActivityManagerService和PhoneWindowManager源码,强制设备开机和按HOME键时启动指定Launcher。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里总结下我研究这个需求,想出的两种解决方案。
第一种方法最简单暴力只要修改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就好了。

原文链接:https://blog.csdn.net/fireness/article/details/48177923

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值