fun initLocalAppList() { GlobalScope.launch(Dispatchers.IO) { synchronized(localAppList) { val packageManager = ContextHolder.get().packageManager val installedApplications = packageManager.getInstalledApplications(0) val selfPkg = ContextHolder.get().packageName localAppList.clear() installedApplications.forEach { if (it.packageName == selfPkg) { return@forEach } if ((it.flags and ApplicationInfo.FLAG_SYSTEM) != 0) { return@forEach } val apkFile = File(it.sourceDir) val isSupport = AbiUtils.isSupport(apkFile) val name = it.loadLabel(packageManager).toString() val icon = it.loadIcon(packageManager) localAppList.add( LocalAppBean( name, it.packageName, icon = icon, isSupport = isSupport ) ) } localAppList.sortBy { it.name } } } }转为java代码
时间: 2024-02-10 09:04:53 浏览: 82
Here's the Java equivalent of the given Kotlin code:
```
public void initLocalAppList() {
GlobalScope.INSTANCE.launch(Dispatchers.IO, new Function2<CoroutineScope, Continuation<? super Unit>, Object>() {
@Override
public Object invoke(CoroutineScope coroutineScope, Continuation<? super Unit> continuation) {
synchronized (localAppList) {
PackageManager packageManager = ContextHolder.INSTANCE.get().getPackageManager();
List<ApplicationInfo> installedApplications = packageManager.getInstalledApplications(0);
String selfPkg = ContextHolder.INSTANCE.get().getPackageName();
localAppList.clear();
for (ApplicationInfo it : installedApplications) {
if (it.packageName.equals(selfPkg)) {
continue;
}
if ((it.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
continue;
}
File apkFile = new File(it.sourceDir);
boolean isSupport = AbiUtils.INSTANCE.isSupport(apkFile);
String name = it.loadLabel(packageManager).toString();
Drawable icon = it.loadIcon(packageManager);
localAppList.add(new LocalAppBean(name, it.packageName, icon, isSupport));
}
Collections.sort(localAppList, new Comparator<LocalAppBean>() {
@Override
public int compare(LocalAppBean o1, LocalAppBean o2) {
return o1.getName().compareTo(o2.getName());
}
});
}
return null;
}
});
}
```
阅读全文