android 自定义应用(普通应用)作为launcher应用

在frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java中

第一步:
在startHomeActivityLocked方法的开始出加入如下代码:
  1. String packageName = SystemProperties.get("thirdparty.launcher.packagename","com.android.launcher");
  2. String className = SystemProperties.get("thirdparty.launcher.classname","com.android.launcher2.Launcher");
  3. if(isPkgInstalled(mContext, packageName) && !isDefaultLauncher(mContext, packageName))
  4. setDefaultLauncher(packageName, className);
第二步:
在frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java这个类中加入如下方法:
[java] view plain copy
  1. public  boolean isPkgInstalled(Context context, String packageName) {  
  2.     if (packageName == null || "".equals(packageName))  
  3.          return false;  
  4.        
  5.      android.content.pm.ApplicationInfo info = null;  
  6.      try {  
  7.          info = context.getPackageManager().getApplicationInfo(packageName, 0);  
  8.          return info != null;  
  9.      } catch (NameNotFoundException e) {  
  10.          return false;  
  11.     }  
  12. }  
  13.   
  14. public boolean isDefaultLauncher(Context context, String packageName) {  
  15.         Intent intent = new Intent(Intent.ACTION_MAIN);  
  16.         intent.addCategory(Intent.CATEGORY_HOME);  
  17.         ResolveInfo res = context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);  
  18.         if (res.activityInfo == null) {  
  19.             // should not happen. A home is always installed, isn't it?  
  20.             return false;  
  21.         }  
  22.         if (res.activityInfo.packageName.equals("android")) {  
  23.             // 有多个桌面程序存在,且未指定默认项时;       
  24.             return false;  
  25.         } else {  
  26.             Slog.i(TAG, "current Launcher packageName = " + res.activityInfo.packageName);  
  27.             return packageName.equals(res.activityInfo.packageName);  
  28.         }  
  29.     }  
  30.   
  31. private void setDefaultLauncher(String packageName, String className) {    
  32.     // get default component     
  33.     Slog.i(TAG, "defautl packageName = " + packageName + ", default className = " + className);    
  34.     if ((packageName != null && packageName.trim().length() > 1) && (className != null && className.trim().length() > 0)) {  
  35.         IPackageManager pm = ActivityThread.getPackageManager();    
  36.             
  37.         //清除当前默认launcher   
  38.         ArrayList<IntentFilter> intentList = new ArrayList<IntentFilter>();    
  39.         ArrayList<ComponentName> cnList = new ArrayList<ComponentName>();    
  40.         mContext.getPackageManager().getPreferredActivities(intentList, cnList, null);    
  41.         IntentFilter dhIF;    
  42.         for(int i = 0; i < cnList.size(); i++)    
  43.         {    
  44.             dhIF = intentList.get(i);    
  45.             if(dhIF.hasAction(Intent.ACTION_MAIN) &&    
  46.             dhIF.hasCategory(Intent.CATEGORY_HOME))     
  47.             {    
  48.                 mContext.getPackageManager().clearPackagePreferredActivities(cnList.get(i).getPackageName());    
  49.             }    
  50.       }    
  51.             
  52.         //获取所有launcher activity   
  53.         Intent intent = new Intent(Intent.ACTION_MAIN);    
  54.         intent.addCategory(Intent.CATEGORY_HOME);    
  55.         List<ResolveInfo> list = new ArrayList<ResolveInfo>();    
  56.         try     
  57.         {    
  58.             list = pm.queryIntentActivities(intent,    
  59.                 intent.resolveTypeIfNeeded(mContext.getContentResolver()),    
  60.                 PackageManager.MATCH_DEFAULT_ONLY,getCurrentUserIdLocked());    
  61.         }catch (RemoteException e) {    
  62.             throw new RuntimeException("Package manager has died", e);    
  63.         }     
  64.         // get all components and the best match    
  65.         IntentFilter filter = new IntentFilter();    
  66.         filter.addAction(Intent.ACTION_MAIN);    
  67.         filter.addCategory(Intent.CATEGORY_HOME);    
  68.         filter.addCategory(Intent.CATEGORY_DEFAULT);    
  69.         final int N = list.size();    
  70.         Slog.d(TAG, "N:::::hyhyhyhy:::: = " + N);    
  71.         ComponentName[] set = new ComponentName[N];    
  72.         int bestMatch = 0;    
  73.         for (int i = 0; i < N; i++)    
  74.         {    
  75.             ResolveInfo r = list.get(i);    
  76.             set[i] = new ComponentName(r.activityInfo.packageName,    
  77.                             r.activityInfo.name);    
  78.             Slog.d(TAG, "r.activityInfo.packageName:::::hyhyhyhy:::: = " + r.activityInfo.packageName);  
  79.             Slog.d(TAG, "r.activityInfo.name:::::hyhyhyhy:::: = " + r.activityInfo.name);  
  80.             if (packageName.equals(r.activityInfo.packageName)) bestMatch = r.match;    
  81.         }    
  82.         //设置默认launcher   
  83.         ComponentName launcher = new ComponentName(packageName, className);    
  84.         try     
  85.         {    
  86.             pm.addPreferredActivity(filter, bestMatch, set, launcher,getCurrentUserIdLocked());    
  87.         } catch (RemoteException e) {    
  88.             throw new RuntimeException("Package manager has died", e);    
  89.         }     
  90.     }    
  91. }   

第三步:
在device/rockchip/rk3288/system.prop中加入如下语句
  1. thirdparty.launcher.packagename=yourpackagename (你要安装的应用的包名)
  2. thirdparty.launcher.classname=yourclassname(你要安装的应用的类名)

第四步:

     这样可以通过修改系统属性thirdparty.launcher.packagename 和thirdparty.launcher.classname动态设置launcher应用,一般是在系统应用添加界面修改(SystemProperties.setthirdparty.launcher.packagename 和thirdparty.launcher.classname,再重新启动,就可以启动自定义的启动应用。








展开阅读全文

没有更多推荐了,返回首页