默认使用特定app作为启动luncher知识

概述:

Android系统启动后不适用原生的luncher作为安卓系统的桌面,而是使用特定的app作为作为系统桌面

详细描述

          ActivityManagerService.java在每次启动时执行,每次都默认启动设定的launcher的app,当然,如果设定的launcher存在的话,设置其他的launcher为默认会无效,因为重新启动时setDefaultLauncher()会将当前默认launcher清除。只有在卸载了设定默认启动的launcher后才能设置其他launcher为默认启动.,而通过包名可以将我们指定的app设定为默认启动的luncher。

代码中packageName 的值根据各自的apk的包名确定

代码实现

diff --git a/android4.4/frameworks/base/services/java/com/android/server/am/ActivityManagerService.java b/android4.4/frameworks/base/services/java/com/android/server/am/ActivityManagerService.java
index 2068333194..3c4a0ec1ab 100755
--- a/android4.4/frameworks/base/services/java/com/android/server/am/ActivityManagerService.java
+++ b/android4.4/frameworks/base/services/java/com/android/server/am/ActivityManagerService.java    
@@ -2955,6 +2955,43 @@ public final class ActivityManagerService extends ActivityManagerNative
         return intent;
     }
  
+      
+       private void setDefaultLauncher() {     
+                       String packageName = null;
+                       packageName = "包名";
+                       ComponentName setHome = null;     
+                       ArrayList homeActivities = new ArrayList();     
+                       ComponentName currentDefaultHome  = mContext.getPackageManager().getHomeActivities(homeActivities);     
+                       if(currentDefaultHome != null)         
+                       {                 
+                               if(currentDefaultHome.getPackageName().equals(packageName))             
+                               {                        
+                                       return;                 
+                               }                 
+                               mContext.getPackageManager().clearPackagePreferredActivities(currentDefaultHome.getPackageName());
               
+                       }         
+                       ComponentName[] mHomeComponentSet = new ComponentName[homeActivities.size()];     
+                       for (int i = 0; i < homeActivities.size(); i++) {                 
+                               final ResolveInfo candidate = homeActivities.get(i);             
+                               final ActivityInfo info = candidate.activityInfo;                 
+                               ComponentName activityName = new ComponentName(info.packageName, info.name);             
+                               mHomeComponentSet[i] = activityName;             
+                               if(info.packageName.equals(packageName)){                         
+                                       setHome = activityName;                 
+                               }         
+                       }         
+                       if(setHome != null){             
+                               IntentFilter mHomeFilter;                 
+                               mHomeFilter = new IntentFilter(Intent.ACTION_MAIN);             
+                               mHomeFilter.addCategory(Intent.CATEGORY_HOME);                 
+                               mHomeFilter.addCategory(Intent.CATEGORY_DEFAULT);                 
+                               mContext.getPackageManager().replacePreferredActivity(mHomeFilter, IntentFilter.MATCH_CATEGORY_EMPTY,             
+                                       mHomeComponentSet, setHome);     
+                       } 
+      
+                }
+      
+
     boolean startHomeActivityLocked(int userId) {
         if (mHeadless) {
             // Added because none of the other calls to ensureBootCompleted seem to fire
@@ -2970,6 +3007,8 @@ public final class ActivityManagerService extends ActivityManagerNative
             // error message and don't try to start anything.
             return false;
         }
+
+               setDefaultLauncher();
         Intent intent = getHomeIntent();
         ActivityInfo aInfo =
             resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);

你可能感兴趣的:(android)