I'm trying to show the list of background running apps in ListView. what I have tried is creating AppAdapter extending ArrayAdapter and used Async task to call the method which returns the list of running services' package names. I posted the code below.

AppAdapter:

public class AppAdapter extends ArrayAdapter<String> {


private List<ActivityManager.RunningServiceInfo> applist =null;
private Context context;
private PackageManager packageManager;
private List<String> applist1 = null;

public AppAdapter(@NonNull Context context, int resource, @NonNull List<String> objects) {
    super(context, resource, objects);

    this.context = context;
    this.applist1 = objects;
    packageManager = context.getPackageManager();
}

@Override
public int getCount() {
    return ((applist1 != null) ? applist1.size() : 0);
}


@Override
public String getItem(int position) {
    return ((applist1 != null) ? applist1.get(position) : null);
}

@Override
public long getItemId(int position) {
    return position;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.list_item, null);
    }

    ActivityManager.RunningServiceInfo data = applist.get(position);
    Log.d("Test", "data is  : " + data);

    if (data != null){
        Log.d("Test", "data is not null : " + data);
        TextView appName = view.findViewById(R.id.applabel);
        TextView packageName = view.findViewById(R.id.pname);
        ImageView iconView = view.findViewById(R.id.appicon);

        ComponentName mComponentName = data.service;
        appName.setText(mComponentName.getClassName());
        packageName.setText(mComponentName.getPackageName());

    } else {
        Log.d("Test", "data is null : " + data);

    }
    return view;
}
}

Code in Main Activity:

private class LoadApplications extends AsyncTask<Void, Void, Void>{

    private ProgressDialog progressDialog = null;

    @Override
    protected Void doInBackground(Void... voids) {

         applist1 = runningServices();
        listadapter = new AppAdapter(Taskmanager.this, R.layout.list_item, applist1);
        Log.d("Test", "applist1 is : " + applist1);
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {

        listView.setAdapter(listadapter);
        progressDialog.dismiss();
        super.onPostExecute(aVoid);
    }

    @Override
    protected void onPreExecute() {
        progressDialog = ProgressDialog.show(Taskmanager.this, null, "Loading..");
        super.onPreExecute();
    }
}

and the runningServices() method correctly returns the list of package names (if you want to see that let me know).

The problem is the after app launch it shows nothing (no list). I didn't forget to call the async task in the onCreate method of the activity.

Can someone please figure out what might be going wrong. I posted only limited code if you want more then ask me in the comments, please.

1 Answers

0
Sultan Mahmud On

Do the necessary work after getting a list from the background task. Return the List value from the doInBackgroind method and then assign it to adapter in the onPostExecute method which runs on the UI thread.

public class LoadApplications extends AsyncTask<Void, Void, List<ActivityManager.RunningServiceInfo>> {

        @Override
        protected List<ActivityManager.RunningServiceInfo> doInBackground(Void... arg0) {
              return runningServices();
        }

        @Override
        protected void onPostExecute(List<ActivityManager.RunningServiceInfo> applist) {
                  // call super
                   listadapter = new AppAdapter(Taskmanager.this, 
                   R.layout.list_item, applist);

                  listView.setAdapter(listadapter);
                  progressDialog.dismiss();

                  //other works 
        }
    }