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 条评论
登录 后参与评论

相关文章

来自专栏向治洪

仿iphone快速导航悬浮球

用过iphone的朋友都知道,iPhone有个圆球辅助工具,它漂浮在你的手机屏幕(在任何APP之上),你可以将它移动到任何地方,它叫做AssistiveTouc...

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

Activity的生命周期

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

1798
来自专栏Android干货

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

2647
来自专栏Spring相关

碎片的动态添加

471
来自专栏Android知识点总结

3-AVI--Activity与Fragment的数据传递

1092
来自专栏Spring相关

接收广播系统

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

551
来自专栏用户画像

Android Activity之间的跳转

1、在应用中增加一个Activity,这需要修改AndroidManifest.xml文件

693
来自专栏GIS讲堂

安卓底部选项卡

腾讯QQ有了更新,在自己的手机上更新了一下,试运行了一段时间,被底部的选项卡给深深的迷住了。于是,有了去做做它的原始冲动与欲望,在历经一个早上的琢磨,终于实现了...

1262
来自专栏james大数据架构

Android中Fragment+ViewPager的配合使用

官方推荐 ViewPager与Fragment一起使用,可以更加方便的管理每个Page的生命周期,这里有标准的适配器实现用于ViewPager和Fragment...

21510
来自专栏Android开发指南

7.首页、bitmaputils

3478

扫码关注云+社区