- <strong>需求就是,获取系统上所有的应用及图标,然后显示在自己的app应用上,然后通过点击对应的图标可以跳转到对应的软件里面去。</strong>
复制代码 1、先获取到系统的app功能
- public List<ApplicationInfo> getAllApps(Context context) {
- List<ApplicationInfo> apps = new ArrayList<>();
- PackageManager packageManager = context.getPackageManager();
- // GET_META_DATA表示获取应用的元数据
- List<ApplicationInfo> installedApps = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
- for (ApplicationInfo appInfo : installedApps) {
- // 通过appInfo.packageName可以获取应用的包名
- // 过滤掉系统应用和非启动器的第三方应用
- if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
- // 非系统应用
- // apps.add(appInfo);
- } else if (isLauncherApp(packageManager, appInfo)) {
- // 系统应用且是启动器应用
- LogUtils.d("appInfo.packageName==:" +appInfo.packageName);
- apps.add(appInfo);
- }
- }
- return apps;
- }
- private boolean isLauncherApp(PackageManager packageManager, ApplicationInfo applicationInfo) {
- Intent intent = new Intent(Intent.ACTION_MAIN);
- intent.addCategory(Intent.CATEGORY_LAUNCHER);
- intent.setPackage(applicationInfo.packageName);
- List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(intent, 0);
- return !resolveInfoList.isEmpty();
- }
复制代码 2、然后把获取到的资源放入到适配器
- public void addProjiectData() {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
- applicationInfoList=new ArrayList<>();
- applicationInfoList=getAllApps(SysAppActivity.this);
- }
- LogUtils.d("applicationInfoList==:" + applicationInfoList.size()+",包名:"+applicationInfoList.get(4).packageName);
- for(int i =0;i<applicationInfoList.size();i++) {
- SysAppResult md = new SysAppResult();
- md.setName(applicationInfoList.get(i).packageName);
- // md.setImgPath(applicationInfoList.get(i).icon);
- sysAppResultList.add(md);
- }
- sysAppAdapter.setDate(sysAppResultList);
- }
复制代码 3、末了在适配器里面表现对应的图标
- final SysAppResult result = mlist.get(position);
- LogUtils.showLogD("name_tv="+result.getName());
- PackageManager pm = context.getPackageManager();
- ApplicationInfo appInfo = null;
- try {
- appInfo = pm.getApplicationInfo(result.getName(), 0);
- Drawable appIcon = pm.getApplicationIcon(appInfo);
- holder.project_img.setImageDrawable(appIcon);
- } catch (PackageManager.NameNotFoundException e) {
- e.printStackTrace();
- }
复制代码 通过这三步骤就可以在本身app表现系统的apk了,跳转的话这个就不说了懂得都懂。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |