使用RecycleView和CardView展示已安装应用的包名和图标
① 获取安装的应用信息
public class ApkInfos {
Context mContext;
public ApkInfos(Context context){
mContext = context;
}
public List getAllInstalledApkInfo(){
List 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 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{
Context mContext;
List mStringlist;
public AppsAdapter(Context context, List 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 appInfos = new ApkInfos(this).GetAllInstalledApkInfo();
AppsAdapter adapter=new AppsAdapter(this,appInfos);
recyclerView.setAdapter(adapter);
效果图: