android4.4设置默认launcher



修改frameworks\base\services\java\com\android\server\am\ActivityManagerService.java
在startHomeActivityLocked函数开始的地方加入下面语句

String packageName = SystemProperties.get("thirdparty.launcher.packagename", "com.android.launcher");
String className = SystemProperties.get("thirdparty.launcher.classname", "com.android.launcher2.Launcher");
if(isPkgInstalled(mContext, packageName) && !isDefaultLauncher(mContext, packageName))
	setDefaultLauncher(packageName, className);

其中"thirdparty.launcher.packagename"和"thirdparty.launcher.classname"是需要在项目的prop文件中指定

thirdparty.launcher.packagename=yourpackagename
thirdparty.launcher.classname=yourclassname

这是上面引用到的函数

public  boolean isPkgInstalled(Context context, String packageName) {
	if (packageName == null || "".equals(packageName))
		 return false;
	 
	 android.content.pm.ApplicationInfo info = null;
	 try {
		 info = context.getPackageManager().getApplicationInfo(packageName, 0);
		 return info != null;
	 } catch (NameNotFoundException e) {
		 return false;
	}
}

public boolean isDefaultLauncher(Context context, String packageName) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        ResolveInfo res = context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
        if (res.activityInfo == null) {
            // should not happen. A home is always installed, isn't it?
            return false;
        }
        if (res.activityInfo.packageName.equals("android")) {
            // 有多个桌面程序存在,且未指定默认项时;     
            return false;
        } else {
			Slog.i(TAG, "current Launcher packageName = " + res.activityInfo.packageName);
            return packageName.equals(res.activityInfo.packageName);
        }
    }

private void setDefaultLauncher(String packageName, String className) {  
	// get default component   
	Slog.i(TAG, "defautl packageName = " + packageName + ", default className = " + className);  
	if ((packageName != null && packageName.trim().length() > 1) && (className != null && className.trim().length() > 0)) {
		IPackageManager pm = ActivityThread.getPackageManager();  
		  
		//清除当前默认launcher 
		ArrayList intentList = new ArrayList();  
		ArrayList cnList = new ArrayList();  
		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 list = new ArrayList();  
		try   
		{  
			list = pm.queryIntentActivities(intent,  
				intent.resolveTypeIfNeeded(mContext.getContentResolver()),  
				PackageManager.MATCH_DEFAULT_ONLY,getCurrentUserIdLocked());  
		}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();  
		Slog.d(TAG, "N:::::hyhyhyhy:::: = " + N);  
		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);  
			Slog.d(TAG, "r.activityInfo.packageName:::::hyhyhyhy:::: = " + r.activityInfo.packageName);
			Slog.d(TAG, "r.activityInfo.name:::::hyhyhyhy:::: = " + r.activityInfo.name);
			if (packageName.equals(r.activityInfo.packageName)) bestMatch = r.match;  
		}  
		//设置默认launcher 
		ComponentName launcher = new ComponentName(packageName, className);  
		try   
		{  
			pm.addPreferredActivity(filter, bestMatch, set, launcher,getCurrentUserIdLocked());  
		} catch (RemoteException e) {  
			throw new RuntimeException("Package manager has died", e);  
		}   
	}  
} 

你可能感兴趣的:(Android开发)