Android5.1 默认主launcher、强制主launcher

一.Android5.1 默认主launcher

1.1.默认launcher属性

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
diff --git a/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java b/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
index 2566663..82c5497 100755
--- a/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -3259,6 +3259,66 @@ public final class ActivityManagerService extends ActivityManagerNative
         }
         return intent;
     }
+   
+    private void setDefaultLauncher() { 
+      
+      
+       String packageName = SystemProperties.get("persist.app.pkgName","com.android.launcher3");
+       String className = SystemProperties.get("persist.app.className","com.android.launcher3.Launcher");
+          
+        Slog.i(TAG, "defautl packageName = " + packageName + ", default className = " + className);
+       
+        
+        if ((packageName != null && packageName.trim().length() > 1) && (className != null && className.trim().length() > 0)) {
+          
+                IPackageManager pm = ActivityThread.getPackageManager();  
+                ArrayList<IntentFilter> intentList = new ArrayList<IntentFilter>(); 
+                ArrayList<ComponentName> cnList = new ArrayList<ComponentName>(); 
+                mContext.getPackageManager().getPreferredActivities(intentList, cnList, null); 
+                IntentFilter dhIF; 
+                for(int i = 0; i < cnList.size(); i++) 
+                { 
+                    dhIF = intentList.get(i); 
+                    if(dhIF.hasAction(Intent.ACTION_MAIN) && 
+                    dhIF.hasCategory(Intent.CATEGORY_HOME))  
+                    { 
+                        mContext.getPackageManager().clearPackagePreferredActivities(cnList.get(i).getPackageName()); 
+                    } 
+               }  
+                Intent intent = new Intent(Intent.ACTION_MAIN); 
+                intent.addCategory(Intent.CATEGORY_HOME); 
+                List<ResolveInfo> list = new ArrayList<ResolveInfo>(); 
+                try  
+                { 
+                    list = pm.queryIntentActivities(intent, 
+                        intent.resolveTypeIfNeeded(mContext.getContentResolver()), 
+                        PackageManager.MATCH_DEFAULT_ONLY,UserHandle.getCallingUserId()); 
+                }catch (RemoteException e) { 
+                    throw new RuntimeException("Package manager has died", e); 
+                }  
+                IntentFilter filter = new IntentFilter(); 
+                filter.addAction(Intent.ACTION_MAIN); 
+                filter.addCategory(Intent.CATEGORY_HOME); 
+                filter.addCategory(Intent.CATEGORY_DEFAULT); 
+                final int N = list.size(); 
+                ComponentName[] set = new ComponentName[N]; 
+                int bestMatch = 0
+                for (int i = 0; i < N; i++) 
+                { 
+                    ResolveInfo r = list.get(i); 
+                    set[i] = new ComponentName(r.activityInfo.packageName,r.activityInfo.name);                  
+                    if (r.match > bestMatch) bestMatch = r.match; 
+                } 
+                ComponentName launcher = new ComponentName(packageName, className); 
+                try  
+                { 
+                    pm.addPreferredActivity(filter, bestMatch, set, launcher,UserHandle.getCallingUserId()); 
+                } catch (RemoteException e) { 
+                    throw new RuntimeException("Package manager has died", e); 
+                }  
+           
+        } 
+    }
  
     boolean startHomeActivityLocked(int userId, String reason) {
         if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
@@ -3268,6 +3328,9 @@ 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);
diff --git a/packages/apps/Settings/src/com/android/settings/HomeSettings.java b/packages/apps/Settings/src/com/android/settings/HomeSettings.java
old mode 100644
new mode 100755
index 6da1848..8364ee0
--- a/packages/apps/Settings/src/com/android/settings/HomeSettings.java
+++ b/packages/apps/Settings/src/com/android/settings/HomeSettings.java
@@ -55,6 +55,7 @@ import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settings.search.Index;
 import com.android.settings.search.Indexable;
 import com.android.settings.search.SearchIndexableRaw;
+import android.os.SystemProperties;
  
 public class HomeSettings extends SettingsPreferenceFragment implements Indexable {
     static final String TAG = "HomeSettings";
@@ -124,6 +125,21 @@ public class HomeSettings extends SettingsPreferenceFragment implements Indexabl
                 mHomeComponentSet, newHome.activityName);
  
         getActivity().setResult(Activity.RESULT_OK);
+       
+        //Log.d("xinhua_launcher","className = "+newHome.activityName.getClassName());
+        //Log.d("xinhua_launcher","packageName = " +newHome.activityName.getPackageName());
+       
+        SystemProperties.set("persist.app.pkgName",newHome.activityName.getPackageName());
+        SystemProperties.set("persist.app.className",newHome.activityName.getClassName());
+       
+        startHomeApk();
+    }
+   
+    void startHomeApk(){
+           Intent intent= new Intent(Intent.ACTION_MAIN);
+               intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+               intent.addCategory(Intent.CATEGORY_HOME);
+               getActivity().startActivity(intent);       
     }
  
     void uninstallApp(HomeAppPreference pref) {

 

二.强制主launcher

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
--- a/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -3268,10 +3268,27 @@ public final class ActivityManagerService extends ActivityManagerNative
             // error message and don't try to start anything.
             return false;
         }
-        Intent intent = getHomeIntent();
+        String className = SystemProperties.get("persist.app.className","com.android.launcher3.Launcher");
+               Intent intent = getHomeIntent();
         ActivityInfo aInfo =
             resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
-        if (aInfo != null) {
+           if (aInfo != null) {
+                    PackageManager pm = mContext.getPackageManager();
+                    Intent newintent = new Intent(Intent.ACTION_MAIN);
+                    newintent.addCategory(Intent.CATEGORY_HOME);
+                    List<ResolveInfo> resolveInfoList = pm.queryIntentActivities(newintent, 0);             
+                    if(resolveInfoList != null){
+                           int size = resolveInfoList.size();
+                           for(int i = 0; i < size; i++){
+                               ResolveInfo rInfo = resolveInfoList.get(i);            
+                               if(rInfo.activityInfo.name.equals(className)){
+                                   aInfo = rInfo.activityInfo;
+                               }
+                           }
+                    }
+                   
+                    Log.d("gatsby","packageName = "+aInfo.applicationInfo.packageName);
+       
             intent.setComponent(new ComponentName(
                     aInfo.applicationInfo.packageName, aInfo.name));
             // Don't do this if the home app is currently being

 

posted @   CrushGirl  阅读(446)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示