android 启动时自动设置default Launcher

设置default Launcher


系统有多个Launcher时,会在packages.xml 设置如下信息:


/data/system/packages.xml


<preferred-activities>
<item name="com.android.ulauncher/.Launcher" match="100000" set="2">
<set name="com.android.launcher/com.android.launcher2.Launcher" />
<set name="com.android.ulauncher/.Launcher" />
<filter>
<action name="android.intent.action.MAIN" />
<cat name="android.intent.category.HOME" />
<cat name="android.intent.category.DEFAULT" />
</filter>
</item>
</preferred-activities>


如何在启动时自动设置default Launcher呢?

方法一:
在PackageManagerService构造函数中,会读取packages.xml文件:mRestoredSettings = mSettings.readLPw();
所以在之前添加设置:


readPermissions();
            
String firstBoot = SystemProperties.get("persist.sys.preactivity"); 
if ("0".equals(firstBoot)) {
    addPreferredActivityForULauncher();
}
SystemProperties.set("persist.sys.preactivity", "1");


mRestoredSettings = mSettings.readLPw();


private void addPreferredActivityForULauncher() {
    IntentFilter filter = new IntentFilter();
    filter.addAction("android.intent.action.MAIN");
    filter.addCategory("android.intent.category.HOME"); 
    filter.addCategory("android.intent.category.DEFAULT");
    ComponentName preActivity = new ComponentName("com.android.ulauncher", "com.android.ulauncher.Launcher"); 
    ComponentName[] set = new ComponentName[] {new ComponentName("com.android.launcher", "com.android.launcher2.Launcher"), preActivity}; 
    mSettings.mPreferredActivities.addFilter(
            new PreferredActivity(filter, IntentFilter.MATCH_CATEGORY_EMPTY, set, preActivity));
    scheduleWriteSettingsLocked();
}

方法二:

在ActivityManagerService 


 boolean startHomeActivityLocked() {
        
        /* launch the defualt launcher when the system boots for the first time */
setDefaultLauncher();

...

}

private void setDefaultLauncher()
	{
	    // get default component
	    String  packageName = SystemProperties.get("ro.sw.defaultlauncherpackage", "no");
	    String  className = SystemProperties.get("ro.sw.defaultlauncherclass", "no");
	    Slog.i(TAG, "defautl packageName = " + packageName + ", default className = " + className);
	    if(!packageName.equals("no") && !className.equals("no")){
	        boolean firstLaunch = SystemProperties.getBoolean("persist.sys.sw.firstLaunch", true);
	        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, "mFirstLaunch = " + mFirstLaunch);
	        if(mFirstLaunch){
        		IPackageManager pm = ActivityThread.getPackageManager();
                
                //clear the current preferred 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());
        			}
        		}
                
                // get all Launcher activities
        		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);
        		}catch (RemoteException e) {
                    throw new RuntimeException("Package manager has died", e);
                } 
                // get all components and the best match
        		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;
        		}
        		// add the default launcher as the preferred launcher
        		ComponentName launcher = new ComponentName(packageName, className);
        		try 
        		{
        			pm.addPreferredActivity(filter, bestMatch, set, launcher);
        		} catch (RemoteException e) {
                    throw new RuntimeException("Package manager has died", e);
                } 
            }
	    }
	}

    
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值