通过使用私有列表appProcessInfos; 正在获取总包名称示例“ com.example.app”,但我需要的是应用程序名称。 另一件事是获取系统应用程序也不需要,只有运行我需要的应用程序详细信息的用户。 在此先感谢您的帮助或建议。
public class ShowAppAdapter extends RecyclerView.Adapter<ShowAppAdapter.ShowAppAdapterViewHolder> {
private List<ActivityManager.RunningAppProcessInfo> appProcessInfos;
public ShowAppAdapter(List<ActivityManager.RunningAppProcessInfo> appProcessInfos){
this.appProcessInfos = appProcessInfos;
}
@NonNull
@Override
public ShowAppAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.appshow_layout, parent,false);
return new ShowAppAdapterViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ShowAppAdapterViewHolder holder, int position) {
String title = String.valueOf(appProcessInfos.get(position).processName);
holder.textView.setText(title);
}
@Override
public int getItemCount() {
return appProcessInfos.size();
}
public class ShowAppAdapterViewHolder extends RecyclerView.ViewHolder {
TextView textView;
ImageView imageView;
public ShowAppAdapterViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
imageView = itemView.findViewById(R.id.imageView);
}
}
}
这是我的适配器类。
答案 0 :(得分:1)
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
PackageManager pm = this.getPackageManager();
List<String> appList=new ArrayList<>();
List<ActivityManager.RunningAppProcessInfo> allTasks = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo allTask : allTasks) {
try {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
allTask.processName, PackageManager.GET_META_DATA));
appList.add(c.toString());
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
现在将appList
传递到您的适配器中,如下所示:
按如下所示制作适配器
public class ShowAppAdapter extends RecyclerView.Adapter<ShowAppAdapter.ShowAppAdapterViewHolder> {
private List<String> appProcessInfos;
public ShowAppAdapter(List<String> appProcessInfos){
this.appProcessInfos = appProcessInfos;
}
@NonNull
@Override
public ShowAppAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.appshow_layout, parent,false);
return new ShowAppAdapterViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ShowAppAdapterViewHolder holder, int position) {
String title = appProcessInfos.get(position);
holder.textView.setText(title);
}
@Override
public int getItemCount() {
return appProcessInfos.size();
}
public class ShowAppAdapterViewHolder extends RecyclerView.ViewHolder {
TextView textView;
ImageView imageView;
public ShowAppAdapterViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
imageView = itemView.findViewById(R.id.imageView);
}
}
}