如果您长按主屏幕并选择添加应用程序快捷方式,您将看到一个显示所有已安装应用程序的 ListView 。我的应用程序需要同样的功能,所以我从启动器源代码中复制了 Intent :
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
this.startActivityForResult(pickIntent, MoreIconsConstants.REQUEST_PICK_APPLICATION)
当它在启动器上执行时,速度非常快。当我在我的 Activity 中执行此操作时,可能需要 15 秒而不是 3 秒。似乎启动器必须将此数据缓存一段时间?有什么方法可以缓存数据吗?
谢谢!
最佳答案
您可以通过此代码读取所有安装的应用
final PackageManager pm = a.getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List<ResolveInfo> apps = pm.queryIntentActivities(mainIntent, 0);
Collections.sort(apps, new ResolveInfo.DisplayNameComparator(pm));
for (int i = 0; i < apps.size(); i++)
{
ResolveInfo info = apps.get(i);
//Intent to start the app
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(info.activityInfo.applicationInfo.packageName,info.activityInfo.name));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
//Load app icon
info.activityInfo.loadIcon(pm)
//Load app label
info.loadLabel(pm)
}
检查此示例代码 http://developer.android.com/resources/samples/Home/src/com/example/android/home/Home.html 搜索 loadApplications 函数
我有同样的问题,所以我所做的是在一个线程中通过上面的代码加载应用程序。
关于android - 打算在我的 Activity 中选择一个安装缓慢的应用程序,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2009013/