android 启动时自动设置default Launcher

转自:http://blog.csdn.net/pediyasm/article/details/7987389

设置default Launcher



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


/data/system/packages.xml















如何在启动时自动设置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();

...

}

[java]  view plain copy print ?
  1. private void setDefaultLauncher()  
  2.     {  
  3.         // get default component  
  4.         String  packageName = SystemProperties.get("ro.sw.defaultlauncherpackage""no");  
  5.         String  className = SystemProperties.get("ro.sw.defaultlauncherclass""no");  
  6.         Slog.i(TAG, "defautl packageName = " + packageName + ", default className = " + className);  
  7.         if(!packageName.equals("no") && !className.equals("no")){  
  8.             boolean firstLaunch = SystemProperties.getBoolean("persist.sys.sw.firstLaunch"true);  
  9.             Slog.d(TAG, "firstLaunch = " + firstLaunch);  
  10.             if(firstLaunch){  
  11.                 mFirstLaunch = true;  
  12.                 // do this only for the first boot   
  13.                 SystemProperties.set("persist.sys.sw.firstLaunch""false");  
  14.             }  
  15.             Slog.d(TAG, "mFirstLaunch = " + mFirstLaunch);  
  16.             if(mFirstLaunch){  
  17.                 IPackageManager pm = ActivityThread.getPackageManager();  
  18.                   
  19.                 //clear the current preferred launcher  
  20.                 ArrayList intentList = new ArrayList();  
  21.                 ArrayList cnList = new ArrayList();  
  22.                 mContext.getPackageManager().getPreferredActivities(intentList, cnList, null);  
  23.                 IntentFilter dhIF;  
  24.                 for(int i = 0; i < cnList.size(); i++)  
  25.                 {  
  26.                     dhIF = intentList.get(i);  
  27.                     if(dhIF.hasAction(Intent.ACTION_MAIN) &&  
  28.                     dhIF.hasCategory(Intent.CATEGORY_HOME))   
  29.                     {  
  30.                         mContext.getPackageManager().clearPackagePreferredActivities(cnList.get(i).getPackageName());  
  31.                     }  
  32.                 }  
  33.                   
  34.                 // get all Launcher activities  
  35.                 Intent intent = new Intent(Intent.ACTION_MAIN);  
  36.                 intent.addCategory(Intent.CATEGORY_HOME);  
  37.                 List list = new ArrayList();  
  38.                 try   
  39.                 {  
  40.                     list = pm.queryIntentActivities(intent,  
  41.                         intent.resolveTypeIfNeeded(mContext.getContentResolver()),  
  42.                         PackageManager.MATCH_DEFAULT_ONLY);  
  43.                 }catch (RemoteException e) {  
  44.                     throw new RuntimeException("Package manager has died", e);  
  45.                 }   
  46.                 // get all components and the best match  
  47.                 IntentFilter filter = new IntentFilter();  
  48.                 filter.addAction(Intent.ACTION_MAIN);  
  49.                 filter.addCategory(Intent.CATEGORY_HOME);  
  50.                 filter.addCategory(Intent.CATEGORY_DEFAULT);  
  51.                 final int N = list.size();  
  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.                     if (r.match > bestMatch) bestMatch = r.match;  
  60.                 }  
  61.                 // add the default launcher as the preferred launcher  
  62.                 ComponentName launcher = new ComponentName(packageName, className);  
  63.                 try   
  64.                 {  
  65.                     pm.addPreferredActivity(filter, bestMatch, set, launcher);  
  66.                 } catch (RemoteException e) {  
  67.                     throw new RuntimeException("Package manager has died", e);  
  68.                 }   
  69.             }  
  70.         }  
  71.     }  

你可能感兴趣的:(Android,转帖)