首页 > 代码库 > 怎样将主菜单图标改成按安装时间排序
怎样将主菜单图标改成按安装时间排序
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的调用
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的调用
怎样将主菜单图标改成按安装时间排序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。