android 开机默认进入指定Launcher

转载一下方便以后参考 原文: https://blog.csdn.net/fireness/article/details/48177923

这里总结下我研究这个需求,想出的两种解决方案。 

第一种方法最简单暴力只要修改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>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

这里就加了一句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.  
  9. if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL

  10. && mTopAction == null) {

  11. // We are running in factory test mode, but unable to find

  12. // the factory test app, so just sit around displaying the

  13. // error message and don't try to start anything.

  14. return false;

  15. }

  16. Intent intent = getHomeIntent();

  17. ActivityInfo aInfo =

  18. resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);

  19. if (aInfo != null) {

  20. //add wwd start

  21. PackageManager pm = mContext.getPackageManager();

  22. Intent newintent = new Intent(Intent.ACTION_MAIN);

  23. newintent.addCategory(Intent.CATEGORY_HOME);

  24.  
  25. List<ResolveInfo> resolveInfoList = pm.queryIntentActivities(newintent, 0);

  26. //判断带有Intent.CATEGORY_HOME标签的所有activity中如果有你指定的activity则替换

  27. if(resolveInfoList != null){

  28. int size = resolveInfoList.size();

  29. for(int i = 0; i < size; i++){

  30. ResolveInfo rInfo = resolveInfoList.get(i);

  31. if(rInfo.activityInfo.name.equals("com.android.launcher3.Launcher")){

  32. aInfo = rInfo.activityInfo;

  33. }

  34. }

  35. }

  36. //add wwd stop

  37. intent.setComponent(new ComponentName(

  38. aInfo.applicationInfo.packageName, aInfo.name));

  39. // Don't do this if the home app is currently being

  40. // instrumented.

  41. aInfo = new ActivityInfo(aInfo);

  42. aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);

  43. ProcessRecord app = getProcessRecordLocked(aInfo.processName,

  44. aInfo.applicationInfo.uid, true);

  45. if (app == null || app.instrumentationClass == null) {

  46. intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);

  47. //这里启动你已替换过的activity

  48. mStackSupervisor.startHomeActivity(intent, aInfo);

  49. }

  50. }

  51.  
  52. return true;

  53. }

  54.  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

这样就把开机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. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值