Tech tip of this week

How to get List of Installed applications

This code will display the list of applications installed on device.


public class AppsList extends ListActivity 
{
      class AppsAdapter extends ArrayAdapter 
      {
              public AppsAdapter(Context context, int resource,                                                                 List appsList) 
              {
                     super(context, resource, appsList);
                     mContext = context;
                     appList = appsList;
                     mPkm = context.getPackageManager();
              }

      @Override
       public View getView(int position, View convertView, ViewGroup parent) 
       {
               View view = convertView;
               if (null == view) 
               {
                   LayoutInflater inflatter = (LayoutInflater) mContext
                   .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                   view = inflatter.inflate(R.layout.apps_list_items, null);
               }
              mAppInfo = appList.get(position);
              if (null != mAppInfo) 
              {
                   ImageView icon = (ImageView) view.findViewById(R.id.icon);
                   icon.setImageDrawable(mAppInfo.loadIcon(mPkm));
                   ((TextView)                                                                                                                  view.findViewById(R.id.appname)).setText(mAppInfo.loadLabel(mPkm));
      }
      return view;
  }
}
private PackageManager mPkm;
private ApplicationInfo mAppInfo;
private Context mContext;
private List appList = null;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          mContext = this;
          mPkm = getPackageManager();
          List appsList = mPkm
          .getInstalledApplications(PackageManager.GET_META_DATA);
          setListAdapter(new AppsAdapter(mContext, R.layout.apps_list_items, appsList));
}
}

No comments:

Post a Comment