Android list installed apps

In this Android tip, I am going to show you how to query and display the installed applications on an Android device. A useful class that is used to achieve this goal is PackageManager. You obtain a PackageManager instance by calling the getPackageManager() method of your activity. From the PackageManager instance, you can get a list of installed apps using the queryIntentActivities() method. You will an Intent object and a flag to the method. The Intent object is created for the ACTION_MAIN action. The category of the Intent will be CATEGORY_LAUNCHER. The PackageManager instance can also be used to get app icon and title using the getApplicationIcon() and getApplicationLabel() method. You will pass to these methods an ApplicationInfo object. The ApplicationInfo object can be obtained from a ResolverInfo instance. The ResolverInfo instance stored information that is returned from resolving an intent against an IntentFilter. In the example application below, the app icons and titles are displayed in a ListView.




list installed apps




How let create an example application on listing installed apps in a ListView. You create a new Android project. In the activity_main.xml file, you add a ListView.

<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
   xmlns:tools= "http://schemas.android.com/tools" android:layout_width="match_parent"
   android:layout_height="match_parent" android:paddingLeft= "@dimen/activity_horizontal_margin"
   android:paddingRight= "@dimen/activity_horizontal_margin"
   android:paddingTop= "@dimen/activity_vertical_margin"
   android:paddingBottom= "@dimen/activity_vertical_margin" tools:context=".MainActivity">
   <ListView
     android:id="@+id/list"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     />
</RelativeLayout>


In the res/layout directory, you create a layout file (row_layout.xml) for the ListView.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   android:background="#000000"
   android:padding="5dip" >

   <ImageView
     android:id="@+id/appicon"
     android:layout_width="30dp"
     android:layout_height="30dp"
     android:padding="5sp"
     android:contentDescription="icon"
     />
   <TextView
     android:id="@+id/appname"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:padding="10sp"
     android:textSize="20sp"
     android:textColor="#ffffff"
     android:textStyle="bold" >
   </TextView>

</LinearLayout>


In the src directory, you create a class called InstalledApp. It is used stored app icons and titles.

import android.graphics.drawable.Drawable;
public class InstalledApp {
   private String appTitle;
   private Drawable appIcon;
   public void setAppTitle(String appTitle){this.appTitle=appTitle;}
   public void setAppIcon(Drawable appIcon){this.appIcon=appIcon;}
   public String getAppTitle(){return appTitle;}
   public Drawable getAppIcon(){return appIcon;}
}


In the src directory, you also create an adapter class (ListAdapter) to supply data to the ListView.

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class ListAdapter extends ArrayAdapter {
   int vg;
   ArrayList<InstalledApp> apps;
   Context context;
   public ListAdapter(Context context, int vg, int id, ArrayList<InstalledApp> apps) {
     super(context, vg, apps);
     this.context = context;
     this.vg = vg;
     this.apps= apps;

   }
   public View getView(int position, View convertView, ViewGroup parent) {
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     View itemView = inflater.inflate(vg, parent, false);
     ImageView imageView = (ImageView) itemView.findViewById(R.id.appicon);
     imageView.setImageDrawable(apps.get(position).getAppIcon());
     TextView textAppName = (TextView) itemView.findViewById(R.id.appname);
     textAppName.setText(apps.get(position).getAppTitle());
     return itemView;
   }
}


Finally, in the MainActivity class, you write code to query the installed apps and display their icons and titles in the ListView.

import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends ActionBarActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     displayInstalledApps();
   }
   public void displayInstalledApps(){
     // Create an ArrayList object to store installed apps
     ArrayList<InstalledApp> apps_list=new ArrayList<InstalledApp>();
     // Create Intent object for ACTION_MAIN
     Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
     // We wish to query the installed apps so let specify CATEGORY_LAUNCHER to the intent's category
     mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
     // Query the installed apps
     PackageManager packManager=getPackageManager();
     List appsList =packManager.queryIntentActivities( mainIntent, 0);
     // Get app titles and icons
     for (Object object : appsList) {

        ResolveInfo info = (ResolveInfo) object;
        Drawable icon= packManager.getApplicationIcon(info.activityInfo.applicationInfo);
        String appTitle=packManager.getApplicationLabel(info.activityInfo.applicationInfo).toString();
        InstalledApp app=new InstalledApp();
        app.setAppTitle(appTitle);
        app.setAppIcon(icon);
        apps_list.add(app);

     }
     // Show the app icons and titles in a ListView
     ListView list=(ListView)findViewById(R.id.list);
     ListAdapter adapter=new ListAdapter(this,R.layout.row_layout,R.id.appname,apps_list);
     list.setAdapter(adapter);

   }
}


Posted by: Dara | post date: 03-29-2015 | Subject: Android Apps Development

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.