我是java新手我试图根据数据使用情况对设备中的安装应用程序列表进行排序。我想在top.i使用代码找到安装包的最高点是:
List<ApplicationItem> mApplicationItemList = new ArrayList<ApplicationItem>();
PackageManager p = getPackageManager();
final List<PackageInfo> apps = p.getInstalledPackages(PackageManager.GET_PERMISSIONS);
for (PackageInfo packageInfo : apps) {
ApplicationItem item = new ApplicationItem();
if (packageInfo.requestedPermissions == null)
continue;
if (Arrays.asList(packageInfo.requestedPermissions).contains(android.Manifest.permission.INTERNET) &&
!Utility.isSystemApp(packageInfo.packageName) &&
(p.getLaunchIntentForPackage(packageInfo.packageName) != null)) {
item.setPkgName(packageInfo.packageName);
mApplicationItemList.add(item);
}
}
and find data usage by this code:
public static long getTotalDataByUid(String pkgName) {
long totalData = 0;
try {
ApplicationInfo appInfo = getApplicationContext().getPackageManager().getApplicationInfo(pkgName, 0);
//TODO Handle UNSUPPORTED
long tx = TrafficStats.getUidTxBytes(appInfo.uid);
long rx = TrafficStats.getUidRxBytes(appInfo.uid);
totalData = tx + rx;
if (totalData < 0) {
totalData = 0;
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
totalData = 0;
}
return totalData;
}
答案 0 :(得分:0)
使用此方法对arraylist进行排序
"def"