-1

by using private List appProcessInfos; am getting the total package name example "com.example.app" but what i need is the app name. Another thing is that am getting the system app also which is not need, only the user running app details i need. Thanks in advance any help or suggestion would be greatly appreciated.

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);

        }
    }



}

this is my Adapter class.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

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();
            }
        }

Now pass appList into your adapter like below :

Make your Adapter like below

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);

        }
    }



}
Rutvik Bhatt
  • 3,185
  • 1
  • 16
  • 28
  • getting error please check it am uploading the screen shot http://i.imgur.com/sa99Gt2.png –  Jun 22 '18 at 09:30
  • app crashed .. http://i.imgur.com/A7JhifN.png Caused by: java.lang.ClassCastException: android.app.ActivityManager$RecentTaskInfo cannot be cast to android.app.ActivityManager$RunningAppProcessInfo –  Jun 22 '18 at 09:45
  • @pixerLiveTV see my edited answer – Rutvik Bhatt Jun 22 '18 at 10:00
  • thanks brother it worked –  Jun 22 '18 at 10:09