登录 / 注册
android 开机默认进入指定Launcher,安卓一开机就启动指定的应用
发布时间:2018-04-16 17:02:23 查看:3016
这里总结下我研究这个需求,想出的两种解决方案。
第一种方法最简单暴力只要修改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。

实测得到的结果是priority的值范围是-1000到1000,设置1000最好了

第二种方法需要修改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就好了。

宁夏银川舜新艺软件开发
ShunXinyi Soft develop
主要经营
软件开发,网站制作,网页设计,移动应用(安卓、苹果),微信,微网站,
FLASH动画,电子商务,计算机软硬件及网络设备等。
电话:18695132945 QQ:23923027
舜新艺软件开发 宁ICP备16001093号-1 宁公网安备 64010602000809号