Android get device installed apps with icons

So we have an app which displays a list of the installed apps on the device from which the user can choose. Basically we display a gridview to the user which contains the app icon and title. To get the list of all apps on the device without the system apps is simple.

We have this util class which help us get the info for all packages we need:

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;


public class InstalledPackagesUtils {
    private static final String TAG =  InstalledPackagesUtils.class.getName();

    public static List installedPackages(PackageManager packageManager) {
        List installedPackages = new ArrayList();

        try {
            List packages = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);

            for (ApplicationInfo applicationInfo : packages) {
                if (!isSystemPackage(applicationInfo)) {
                    if (!applicationInfo.packageName.equals(Constants.PACKAGE_NAME))
                        installedPackages.add(applicationInfo);
                }
            }
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }

        return installedPackages;
    }

    private static boolean isSystemPackage(ApplicationInfo applicationInfo) {
        return ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
    }

}

 

And we have the following adapter to add apps to the gridview:

 

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

!!!import .R;

import java.util.ArrayList;
import java.util.List;


public class UserAppsAdapter extends BaseAdapter {
    private static final String TAG = UserAppsAdapter.class.getName();

    private List appsList = new ArrayList<>();
    private Context ctx;
    private PackageManager packageManager;
    private LayoutInflater layoutInflater;

    public UserAppsAdapter(Context ctx, List appsList) {
        this.ctx = ctx;
        this.appsList = appsList;
        this.packageManager = ctx.getPackageManager();
        layoutInflater = (LayoutInflater) ctx
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View view=convertView;
        ViewHolder viewHolder;

        ApplicationInfo data = getItem(position);

        if(view == null) {
            view = layoutInflater.inflate(R.layout.layout_user_app_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.icon = (ImageView) view.findViewById(R.id.app_icon);
            viewHolder.name = (TextView) view.findViewById(R.id.app_name);

            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.icon.setImageDrawable(data.loadIcon(packageManager));
        viewHolder.name.setText(data.loadLabel(packageManager));

        return view;
    }

    @Override
    public int getCount() {
        return appsList.size();
    }

    @Override
    public ApplicationInfo getItem(int position) {
        return appsList.get(position);
    }

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

    private class ViewHolder {
        ImageView icon;
        TextView name;
    }
}

Basically the method which loads the icon is the loadIcon method of the ApplicationInfo class. This method accepts the package manager and returns a Drawable. So data.loadIcon(packageManager) is the tricky stuff.
Otherwise there is a great tutorial here:
https://github.com/javatechig/Advance-Android-Tutorials/tree/master/ListInstalledApps and the code of the android launcher: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/4.2_r1/com/android/launcher2/Launcher.java

You may also like...