• 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.     }  





  • 相关阅读:
    SharePoint 2013 工作流之使用Visio设计篇
    SharePoint 2013 工作流之Visual Studio开发示例篇
    Ajax
    jq判断上下滚动
    元素(绝对定位)水平垂直居中
    css3动画:animation
    自定义button上传按钮
    Json
    页面滚动到顶部
    HDU 4358 Boring counting dfs序+莫队算法
  • 原文地址:https://www.cnblogs.com/liulaolaiu/p/11744535.html
Copyright © 2020-2023  润新知