码迷,mamicode.com
首页 > 移动开发 > 详细

android 如何将主菜单图标改成按安装时间排序

时间:2014-09-05 10:07:01      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:android   io   java   ar   cti   代码   sp   on   c   

1. 在 LauncherModel.java 中加入如下代码, 如果是KK Launcher3 ApplicationInfo要替换为AppInfo
public static final Comparator<ApplicationInfo> getInstallTimeComparator() {
  return new Comparator<ApplicationInfo>() {
            public final int compare(ApplicationInfo a, ApplicationInfo b) {
                return a.firstInstallTime == b.firstInstallTime ? 0 : a.firstInstallTime > b.firstInstallTime ? 1:-1;
            }
        };
 }


 public static class InstallTimeComparator implements Comparator<ResolveInfo> {
  private PackageManager mPackageManager;
  List<ResolveInfo> mapps;
  InstallTimeComparator(PackageManager pm, List<ResolveInfo> apps)
  {
   mPackageManager = pm;
   mapps = apps;
   
  }
 
  public final int compare(ResolveInfo a, ResolveInfo b) {
   String packageNameA = a.activityInfo.applicationInfo.packageName;
   String packageNameB = b.activityInfo.applicationInfo.packageName;
   long firstInstallTimeA = 0, firstInstallTimeB = 0;
   try{
   firstInstallTimeA = mPackageManager.getPackageInfo(packageNameA, 0).firstInstallTime;
   firstInstallTimeB = mPackageManager.getPackageInfo(packageNameB, 0).firstInstallTime;
   }catch(Exception e){
    e.printStackTrace(); 
    return 0;
   }
  return firstInstallTimeA == firstInstallTimeB ? 0 : firstInstallTimeA > firstInstallTimeB ? 1:-1;
  }
 };
 
2. 修改 LauncherModel.java 的 loadAllAppsByBatch() 方法:

Collections.sort(apps,new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
替换成:
Collections.sort(apps,new InstallTimeComparator(packageManager, apps));
 
3. 修改 AppsCustomizePagedview.java 的 setApps 方法

Collections.sort(mApps, LauncherModel.getAppNameComparator());
替换成:
Collections.sort(mApps, LauncherModel.getInstallTimeComparator());
 
4. 系统排序会调用reorderApps方法,因此您在其他条件下也要达到安装时间排序,请在对应的reorderApps方法调用前增加
Collections.sort(mApps, LauncherModel.getInstallTimeComparator());
例如:安装的应用后也想安装新的排序方式排列,需要在AppsCustomizePagedview.java 的 addApps 方法前增加sort的调用
 

android 如何将主菜单图标改成按安装时间排序

标签:android   io   java   ar   cti   代码   sp   on   c   

原文地址:http://blog.csdn.net/sergeycao/article/details/39025961

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!