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干货园

Android 高仿微信发朋友圈浏览图片效果

版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/lyhhj/article/details/50...

3121
来自专栏向治洪

关于ListView中包含EditText数据复用引起异常的解决方案

概述 前几天测试提了一个bug,在ListView中添加留言信息,导致错乱的问题。实际上就是ListView需要添加一个EditText,复用导致错乱的问题,这...

2199
来自专栏Android干货

安卓开发_慕课网_ViewPager实现Tab(App主界面)

2767
来自专栏向治洪

StickyListHeaders的使用

我们知道在ios中字母的导航有悬停的效果,在android中,git上有大神实现了这种悬停的功能,我们只要将普通的Listview改为StickyListHe...

2335
来自专栏james大数据架构

Activity间中使用Intent传值

主页面用来输入一个值传入第二个页面显示,关闭第二个页面返回一个值 主页布局: <RelativeLayout xmlns:android="http://sch...

18910
来自专栏图像识别与深度学习

《Android》Lesson13-ListView

22311
来自专栏Spring相关

接收广播系统

New - > Other ->Broadcast Reciver 新建一个BootCompleteReceiver:

661
来自专栏Spring相关

发送自定义广播

这时候启动两个程序,都可以接收到按钮发出的消息,这时候还是标准广播,如果要改为有序广播需要在BroadcastTest项目点击事件中更改:

1002
来自专栏向治洪

android上拉下拉加载更多数据

最近项目中用到了ListView的下拉刷新的功能,总结了一下前辈们的代码,单独抽取出来写了一个demo作为示例。 效果图 下拉刷新: ? 加载更多: ? Cu...

3196
来自专栏软件开发 -- 分享 互助 成长

Activity的生命周期

前言: 接触Android快两个礼拜了,虽然已开发了一个非常简单的app,但却还没有理清Activity的生命周期,自然对它里面的几种方法的含义没有彻底清楚的认...

1848

扫码关注云+社区