android ROM设置默认Launcher(主屏幕应用)


修改线2016年9月29日 17:59:31
+++++++++++++++++++++++
有个国外推荐的标准方案 stackoverflow
+++++++++++++++++++++++



当系统初始化完毕后会进入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();
    ...
}
private void setDefaultLauncher() {
// get default component 
    String packageName = "com.android.launcher";//默认launcher包名
    String className = "com.android.launcher2.Launcher";默认launcher入口
    Slog.i(TAG, "defautl packageName = " + packageName + ", default className = " + className);
    if ((packageName != null && packageName.trim().length() > 1) && (className != null && className.trim().length() > 0)) {
         boolean firstLaunch = SystemProperties.getBoolean("persist.sys.sw.firstLaunch", true); //只做一次 可以注意这几个变量firstLaunch mFirstLaunch
    Slog.d(TAG, "firstLaunch = " + firstLaunch);
    if(firstLaunch){
       mFirstLaunch = true;
       // do this only for the first boot 
       SystemProperties.set("persist.sys.sw.firstLaunch", "false");
    }
    Slog.d(TAG, "firstLaunch = " + firstLaunch);
    if(mFirstLaunch){
        IPackageManager pm = ActivityThread.getPackageManager();
        //清除当前默认launcher
        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());
      }
        }
     //获取所有launcher activity
       Intent intent = new Intent(Intent.ACTION_MAIN);
      intent.addCategory(Intent.CATEGORY_HOME);
       List<ResolveInfo> list = new ArrayList<ResolveInfo>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值