Android – 获取已安装程序的包名与图标

使用RecycleView和CardView展示已安装应用的包名和图标

获取安装的应用信息

public class ApkInfos {

    Context mContext;

    public ApkInfos(Context context){
        mContext = context;
    }

    public List<String> getAllInstalledApkInfo(){
        List<String> apkPackageName = new ArrayList<>();
        Intent intent = new Intent(Intent.ACTION_MAIN,null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED );
        List<ResolveInfo> resolveInfoList = mContext.getPackageManager().queryIntentActivities(intent,0);
        for(ResolveInfo resolveInfo : resolveInfoList){
            ActivityInfo activityInfo = resolveInfo.activityInfo;
            if(!isSystemPackage(resolveInfo)){
                apkPackageName.add(activityInfo.applicationInfo.packageName);
            }
        }
        return apkPackageName;
    }

    public boolean isSystemPackage(ResolveInfo resolveInfo){
        return ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
    }

    public Drawable getAppIconByPackageName(String ApkTempPackageName){
        Drawable drawable;
        try{
            drawable = mContext.getPackageManager().getApplicationIcon(ApkTempPackageName);
        }
        catch (PackageManager.NameNotFoundException e){
            e.printStackTrace();
            drawable = ContextCompat.getDrawable(mContext, R.mipmap.ic_launcher);
        }
        return drawable;
    }

    public String getAppName(String ApkPackageName){
        String Name = "";
        ApplicationInfo applicationInfo;
        PackageManager packageManager = mContext.getPackageManager();
        try {
            applicationInfo = packageManager.getApplicationInfo(ApkPackageName, 0);
            if(applicationInfo!=null){
                Name = (String)packageManager.getApplicationLabel(applicationInfo);
            }
        }catch (PackageManager.NameNotFoundException e) {

            e.printStackTrace();
        }
        return Name;
    }
}

Adapter添加数据

public class AppsAdapter extends RecyclerView.Adapter<AppsAdapter.ViewHolder>{

    Context mContext;
    List<String> mStringlist;

    public AppsAdapter(Context context, List<String> list){

        mContext = context;

        mStringlist = list;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        public CardView cardView;
        public ImageView imageView;
        public TextView textView_App_Name;
        public TextView textView_App_Package_Name;

        public ViewHolder (View view){

            super(view);

            cardView = (CardView) view.findViewById(R.id.card_view);
            imageView = (ImageView) view.findViewById(R.id.imageview);
            textView_App_Name = (TextView) view.findViewById(R.id.id_tv_apk_name);
            textView_App_Package_Name = (TextView) view.findViewById(R.id.id_apk_package_name);
        }
    }

    @Override
    public AppsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){

        View view = LayoutInflater.from(mContext).inflate(R.layout.show_install_app_cardview_layout,parent,false);

        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position){

        ApkInfos apkInfos = new ApkInfos(mContext);

        final String ApplicationPackageName = (String) mStringlist.get(position);
        String ApplicationLabelName = apkInfos.getAppName(ApplicationPackageName);
        Drawable drawable = apkInfos.getAppIconByPackageName(ApplicationPackageName);
        viewHolder.textView_App_Name.setText(ApplicationLabelName);
        viewHolder.textView_App_Package_Name.setText(ApplicationPackageName);
        viewHolder.imageView.setImageDrawable(drawable);
        //添加点击事件直接打开已安装的应用
        viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(ApplicationPackageName);
                if(intent != null){
                    mContext.startActivity(intent);
                }else {
                    Toast.makeText(mContext,ApplicationPackageName + "应用出错了啊", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    @Override
    public int getItemCount(){

        return mStringlist.size();
    }

}

使用Adapter

LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
List<String> appInfos = new ApkInfos(this).GetAllInstalledApkInfo();
AppsAdapter adapter=new AppsAdapter(this,appInfos);
recyclerView.setAdapter(adapter);

效果图:

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏向治洪

Android 纵向跑马灯滚动效果

像淘宝和京东都会有跑马灯的效果,今天给大家贡献下以前项目的一个demo,各位看官,且看效果图。 ? ? ? 我们先定义一个Bean文件,这个实体类文件主要包含标...

2007
来自专栏ASP.NET MVC5 后台权限管理系统

.Net 转战 Android 4.4 日常笔记(10)--PullToRefresh下拉刷新使用

这里我们使用一个开源的库叫:PullToRefresh 开源地址:https://github.com/chenyoca/pull-to-refresh 下载地...

1788
来自专栏项勇

笔记42 | Android加载器Adapter的深入学习篇(一)

1294
来自专栏向治洪

ExpandableListView简单应用及listview模拟ExpandableListView

  首先我们还是来看一些案例,还是拿搜狐新闻客户端,因为我天天上下班没事爱看这个东东,上班又没时间看新闻,上下班路途之余浏览下新闻打发时间嘛. ? ? 看这...

3437
来自专栏Android开发指南

14.不同条目的listview

26310
来自专栏吴小龙同學

Android更换主题

来自开源项目:https://github.com/lguipeng/Notes,其中有改变主题的功能,我做了部分修改,并应用到我的APP中。感谢lguipe...

2915
来自专栏Android干货

万能适配器

3289
来自专栏潇涧技术专栏

Android Development Code Snippets

631
来自专栏蜉蝣禅修之道

Android之共享已安装的apk应用

2555
来自专栏木头编程 - moTzxx

博客园客户端设计

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011415782/article/de...

744

扫码关注云+社区