android 设置默认launcher 附上代码

当系统初始化完毕后会进入homeactivity:
具体代码流程:
ActivityManagerService.java -->
public void systemReady(final Runnable goingCallback) {
...
mMainStack.resumeTopActivityLocked(null);
...
}

ActivityStack.java
final boolean resumeTopActivityLocked(ActivityRecord prev) {
        return resumeTopActivityLocked(prev, null);
    }

final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
...
return mService.startHomeActivityLocked(mCurrentUser); //开始启动homeactivity了,
...
}

然后我们可以在最开始加入一个自己写的接口,来设置默认launcher:
boolean startHomeActivityLocked(int userId) {
    ...
    setDefaultLauncher();
    ...

}

  1. private void setDefaultLauncher() {    
  2.         // get default component     
  3.         String packageName = "com.android.launcher";//默认launcher包名  
  4.         String className = "com.android.launcher2.Launcher";默认launcher入口  
  5.         Slog.i(TAG, "defautl packageName = " + packageName + ", default className = " + className);    
  6.         if ((packageName != null && packageName.trim().length() > 1) && (className != null && className.trim().length() > 0)) {  
  7.             boolean firstLaunch = SystemProperties.getBoolean("persist.sys.sw.firstLaunch"true);  //只做一次 可以注意这几个变量firstLaunch mFirstLaunch  
  8.             Slog.d(TAG, "firstLaunch = " + firstLaunch);    
  9.             if(firstLaunch){  
  10.                 mFirstLaunch = true;  
  11.                 // do this only for the first boot     
  12.                 SystemProperties.set("persist.sys.sw.firstLaunch""false");    
  13.             }    
  14.             Slog.d(TAG, "firstLaunch = " + firstLaunch);    
  15.             if(mFirstLaunch){    
  16.                 IPackageManager pm = ActivityThread.getPackageManager();    
  17.                     
  18.                 //清除当前默认launcher   
  19.                 ArrayList<IntentFilter> intentList = new ArrayList<IntentFilter>();    
  20.                 ArrayList<ComponentName> cnList = new ArrayList<ComponentName>();    
  21.                 mContext.getPackageManager().getPreferredActivities(intentList, cnList, null);    
  22.                 IntentFilter dhIF;    
  23.                 for(int i = 0; i < cnList.size(); i++)    
  24.                 {    
  25.                     dhIF = intentList.get(i);    
  26.                     if(dhIF.hasAction(Intent.ACTION_MAIN) &&    
  27.                     dhIF.hasCategory(Intent.CATEGORY_HOME))     
  28.                     {    
  29.                         mContext.getPackageManager().clearPackagePreferredActivities(cnList.get(i).getPackageName());    
  30.                     }    
  31.               }    
  32.                     
  33.                 //获取所有launcher activity   
  34.                 Intent intent = new Intent(Intent.ACTION_MAIN);    
  35.                 intent.addCategory(Intent.CATEGORY_HOME);    
  36.                 List<ResolveInfo> list = new ArrayList<ResolveInfo>();    
  37.                 try     
  38.                 {    
  39.                     list = pm.queryIntentActivities(intent,    
  40.                         intent.resolveTypeIfNeeded(mContext.getContentResolver()),    
  41.                         PackageManager.MATCH_DEFAULT_ONLY,UserHandle.getCallingUserId());    
  42.                 }catch (RemoteException e) {    
  43.                     throw new RuntimeException("Package manager has died", e);    
  44.                 }     
  45.                 // get all components and the best match    
  46.                 IntentFilter filter = new IntentFilter();    
  47.                 filter.addAction(Intent.ACTION_MAIN);    
  48.                 filter.addCategory(Intent.CATEGORY_HOME);    
  49.                 filter.addCategory(Intent.CATEGORY_DEFAULT);    
  50.                 final int N = list.size();    
  51.                 Slog.d(TAG, "N:::::hyhyhyhy:::: = " + N);    
  52.                 ComponentName[] set = new ComponentName[N];    
  53.                 int bestMatch = 0;    
  54.                 for (int i = 0; i < N; i++)    
  55.                 {    
  56.                     ResolveInfo r = list.get(i);    
  57.                     set[i] = new ComponentName(r.activityInfo.packageName,    
  58.                                     r.activityInfo.name);    
  59.                     Slog.d(TAG, "r.activityInfo.packageName:::::hyhyhyhy:::: = " + r.activityInfo.packageName);  
  60.                     Slog.d(TAG, "r.activityInfo.name:::::hyhyhyhy:::: = " + r.activityInfo.name);  
  61.                     if (r.match > bestMatch) bestMatch = r.match;    
  62.                 }    
  63.                 //设置默认launcher   
  64.                 ComponentName launcher = new ComponentName(packageName, className);    
  65.                 try     
  66.                 {    
  67.                     pm.addPreferredActivity(filter, bestMatch, set, launcher,UserHandle.getCallingUserId());    
  68.                 } catch (RemoteException e) {    
  69.                     throw new RuntimeException("Package manager has died", e);    
  70.                 }     
  71.             }    
  72.         }    
  73.     }  





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值