January 2, 2013

查找并取代默认launcher

PackageManager pm = mContext.getPackageManager();
Intent homeIntent = new Intent(Intent.ACTION_MAIN, null);
homeIntent.addCategory(Intent.CATEGORY_HOME);
List<ResolveInfo> resolvedInfos = pm.queryIntentActivities(homeIntent, 0);

List <ComponentName>  launchComponents = Lists.newArrayList();
    ComponentName xxxTVlauncherComponent = null;
if (resolvedInfos != null) {
    for (ResolveInfo info : resolvedInfos) {
        if (info.activityInfo != null && info.activityInfo.packageName != null) {
            pm.clearPackagePreferredActivities(info.activityInfo.packageName);
            ComponentName component = new ComponentName(info.activityInfo.packageName,info.activityInfo.name);
            launchComponents.add(component);
            if (info.activityInfo.packageName.equals("com.xxxtv.launcher")) {
                xxxTVlauncherComponent = component;
            }
        }
    }
}

if(xxxTVlauncherComponent != null) {
    IntentFilter homeFilter = new IntentFilter();
     homeFilter.addAction(Intent.ACTION_MAIN);
     homeFilter.addCategory(Intent.CATEGORY_HOME);
     homeFilter.addCategory(Intent.CATEGORY_DEFAULT);
     ComponentName[] array = new ComponentName[launchComponents.size()];
     int i = 0;
     for(ComponentName comp : launchComponents) {
        array[i++] = comp;
     }
     pm.addPreferredActivity(homeFilter, 1081344, array,xxxtvLaunchComponent); 
}