AI手机网,短视频直播 硬改改机 一键新机 群控软件 刷机定制

 找回密码
 立即注册
搜索
查看: 7951|回复: 2

android 安卓手机开机默认进入指定桌面或者启动器 Launcher

[复制链接]
发表于 2018-1-31 10:29:38 | 显示全部楼层 |阅读模式

这里总结下我研究这个需求,想出的两种解决方案。
第一种方法最简单暴力只要修改apk的AndroidManifest直接上源码

    1. <activity
    2.             android:name="com.android.launcher3.Launcher"
    3.             android:launchMode="singleTask"
    4.             android:clearTaskOnLaunch="true"
    5.             android:stateNotNeeded="true"
    6.             android:theme="@style/Theme"
    7.             android:windowSoftInputMode="adjustPan"
    8.             android:screenOrientation="nosensor">
    9.             <intent-filter android:priority="2">
    10.                 <action android:name="android.intent.action.MAIN" />
    11.                 <category android:name="android.intent.category.HOME" />
    12.                 <category android:name="android.intent.category.DEFAULT" />
    13.                 <category android:name="android.intent.category.MONKEY"/>
    14.             </intent-filter>
    15. </activity>
    复制代码

这里就加了一句android:priority=”2”,这样在开机和按HOME键时候系统intent判断到category.HOME属性后如果有多个此属性apk,则会进入ResolverActivity让用户选择。当你定义了此优先级它其他未定义的都默认为0,所以优先进入了你的activity。

第二种方法需要修改framework源码来强制进入你的launcher
首先ActivityManagerService.java中

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

    8.         if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL
    9.                 && mTopAction == null) {
    10.             // We are running in factory test mode, but unable to find
    11.             // the factory test app, so just sit around displaying the
    12.             // error message and don't try to start anything.
    13.             return false;
    14.         }
    15.         Intent intent = getHomeIntent();
    16.         ActivityInfo aInfo =
    17.             resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
    18.         if (aInfo != null) {
    19.             //add wwd start
    20.             PackageManager pm = mContext.getPackageManager();
    21.             Intent newintent = new Intent(Intent.ACTION_MAIN);
    22.             newintent.addCategory(Intent.CATEGORY_HOME);

    23.             List<ResolveInfo> resolveInfoList = pm.queryIntentActivities(newintent, 0);
    24.             //判断带有Intent.CATEGORY_HOME标签的所有activity中如果有你指定的activity则替换
    25.             if(resolveInfoList != null){
    26.                 int size = resolveInfoList.size();
    27.                 for(int i = 0; i < size; i++){
    28.                     ResolveInfo rInfo = resolveInfoList.get(i);
    29.                     if(rInfo.activityInfo.name.equals("com.android.launcher3.Launcher")){
    30.                         aInfo = rInfo.activityInfo;
    31.                     }
    32.                 }
    33.             }
    34.             //add wwd stop
    35.             intent.setComponent(new ComponentName(
    36.                     aInfo.applicationInfo.packageName, aInfo.name));
    37.             // Don't do this if the home app is currently being
    38.             // instrumented.
    39.             aInfo = new ActivityInfo(aInfo);
    40.             aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
    41.             ProcessRecord app = getProcessRecordLocked(aInfo.processName,
    42.                     aInfo.applicationInfo.uid, true);
    43.             if (app == null || app.instrumentationClass == null) {
    44.                 intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
    45.                 //这里启动你已替换过的activity
    46.                 mStackSupervisor.startHomeActivity(intent, aInfo);
    47.             }
    48.         }

    49.         return true;
    50.     }
    复制代码

这样就把开机homeactivity替换成你想要启动的luncher了。
下面在修改按home键强制退回的launcher
PhoneWindowManager.java

    1. public void init(Context context, IWindowManager windowManager,
    2.             WindowManagerFuncs windowManagerFuncs) {
    3.             ......
    4.             mHomeIntent =  new Intent(Intent.ACTION_MAIN, null);
    5.         mHomeIntent.addCategory(Intent.CATEGORY_HOME);
    6.         //add wwd start
    7.         ComponentName mHomecom = new ComponentName("com.android.launcher3", "com.android.launcher3.Launcher");
    8.         mHomeIntent.setComponent(mHomecom);
    9.         //add wwd stop
    10.         mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
    11.                 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    12.             ......
    13. }
    复制代码

这里添加以上两句让home强制跳转指定launcher就好了。


发表于 2018-9-3 11:47:43 | 显示全部楼层

学习学习,谢谢分享………………
发表于 2019-10-5 16:43:58 | 显示全部楼层
没有root,安装新的启动器也没法设为默认启动器
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

技术交流售后群

QQ|小黑屋|手机版|站点找错-建议|AI手机网 |Sitemap



GMT+8, 2024-3-29 07:51 , Processed in 0.148463 second(s), 27 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表