gpt4 book ai didi

android - 如何在android中明智地对已安装的应用程序进行日期排序

转载 作者:太空狗 更新时间:2023-10-29 13:26:19 25 4
gpt4 key购买 nike

private List<App> loadInstalledApps(boolean includeSysApps) {
PackageManager appInfo = getPackageManager();
List<ApplicationInfo> listInfo = appInfo.getInstalledApplications(PackageManager.GET_META_DATA);
Collections.sort(listInfo, new ApplicationInfo.DisplayNameComparator(appInfo));

List<App> data = new ArrayList<App>();
for (int index = 0; index < listInfo.size(); index++) {
try {

ApplicationInfo content = listInfo.get(index);
if ((content.flags != ApplicationInfo.FLAG_SYSTEM) && content.enabled) {
if (content.icon != 0) {
App item = new App();

if(!item.isFavourite())
{
item.setTitle(getPackageManager().getApplicationLabel(content).toString());
item.setPackageName(content.packageName);
item.setIcon(getPackageManager().getDrawable(content.packageName, content.icon, content));
long installed = appInfo.getPackageInfo(content.packageName, 0).firstInstallTime;
Date installedDate = new Date(installed);

// create a date time formatter
SimpleDateFormat formatter = new SimpleDateFormat(
"dd/MM/yyyy");
String firstInstallDate = formatter.format(installedDate);
item.setSize(firstInstallDate);
data.add(item);
}
else if(item.isFavourite())
{
item.setTitle(getPackageManager().getApplicationLabel(content).toString());
item.setPackageName(content.packageName);
item.setIcon(getPackageManager().getDrawable(content.packageName, content.icon, content));
data.add(item);

}
}
}
} catch (Exception e) {

}
}
return data;
}

最佳答案

使用下面的比较器代替 DisplayNameComparator

public static class InstallTimeComparator implements Comparator<ApplicationInfo> {

private final PackageManager mPackageManager;

public InstallTimeComparator(PackageManager packageManager) {
mPackageManager = packageManager;
}

@Override
public int compare(ApplicationInfo lhs, ApplicationInfo rhs) {
try {
long lhsInstallTime = mPackageManager.getPackageInfo(lhs.packageName, 0).firstInstallTime;
long rhsInstallTime = mPackageManager.getPackageInfo(rhs.packageName, 0).firstInstallTime;
if (lhsInstallTime < rhsInstallTime) {
return -1;
} else if (rhsInstallTime < lhsInstallTime) {
return 1;
} else {
return 0;
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return 0;
}
}
}

用法如下:

Collections.sort(listInfo, new InstallTimeComparator(appInfo));

如果要倒序,按如下操作:

Collections.sort(listInfo, Collections.reverseOrder(new InstallTimeComparator(appInfo)));

我没有检查上面的代码,所以它可能包含一些错误,但你会明白的。

关于android - 如何在android中明智地对已安装的应用程序进行日期排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21013523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com