【问题标题】:How to get the name of the running app, not the package name? [duplicate]如何获取正在运行的应用程序的名称,而不是包名称? [复制]
【发布时间】:2018-06-22 09:16:41
【问题描述】:

通过使用私有列表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);

        }
    }



}

这是我的适配器类。

【问题讨论】:

标签: java android performance android-fragments


【解决方案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);

        }
    }



}

【讨论】:

  • 收到错误请检查它正在上传屏幕截图i.imgur.com/sa99Gt2.png
  • 应用程序崩溃.. i.imgur.com/A7JhifN.png 原因:java.lang.ClassCastException: android.app.ActivityManager$RecentTaskInfo 无法转换为 android.app.ActivityManager$RunningAppProcessInfo
  • @pixerLiveTV 查看我编辑的答案
  • 感谢兄弟成功
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-28
  • 1970-01-01
  • 1970-01-01
  • 2012-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多