android 开机默认进入指定Launcher

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

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

第一种方法最简单暴力只要修改apk的AndroidManifest直接上源码

 
  1. android:name="com.android.launcher3.Launcher"

  2. android:launchMode="singleTask"

  3. android:clearTaskOnLaunch="true"

  4. android:stateNotNeeded="true"

  5. android:theme="@style/Theme"

  6. android:windowSoftInputMode="adjustPan"

  7. android:screenOrientation="nosensor">

  • 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 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就好了。

你可能感兴趣的:(android 开机默认进入指定Launcher)