2019独角兽企业重金招聘Python工程师标准>>>
项目中, 我们经常要用到显示系统已安装的应用, 可以通过两种方式获取已安装的应用, 一种是通过ApplicationInfo, 一种是通过ResolveInfo. 这里用的是ResolveInfo, 上代码。
<!-- lang: java -->
PackageManager packageMgr = getActivity().getPackageManager();Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);List<ResolveInfo> resovleInfos = packageMgr.queryIntentActivities(mainIntent, 0);for (ResolveInfo resolve : resovleInfos) {// 自定义的类CarAllInstalledAppInfo app = new CarAllInstalledAppInfo();// resolve.loadIcon(packageMgr) 获取应用程序图标app.setAppIcon(resolve.loadIcon(packageMgr));// resolve.loadLabel(packageMgr) 获取应用程序名称app.setAppName(resolve.loadLabel(packageMgr).toString());// resolve.loadLabel(packageMgr) 获取应用程序报名app.setAppPackage(resolve.activityInfo.packageName);// resolve.loadLabel(packageMgr) 获取应用程序启动的第一个activityapp.setEntranceActivity(resolve.activityInfo.name);apps.add(app);}// 打开应用程序ComponentName component = new ComponentName(app.getAppPackage(), app.getEntranceActivity());Intent intent = new Intent(Intent.ACTION_MAIN);// 此处需要填写, 否则会出现打开Contact.apk nullpointer异常intent.addCategory(Intent.CATEGORY_LAUNCHER);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);intent.setComponent(component);startActivity(intent);