How to get information about all the applications installed in your ANDROID Emulator or Phone? / How to use getPackageManager() in ANDROID ?

By | April 3, 2011

Hi all,
In this tutorial I will show you how to get the information about all the applications installed in your ANDROID phone.
What you have to do is create a ANDROID project inside the package pack.GetAllInstalledApplications amd name the java file “GetAllInstalledApplicationsExample.java” and copy the following code into it.

Here the “getPackageManager().getInstalledPackages(0);” gets information about all the packages installed in your ANDROID Phone or emulator.
Now go on and try this.

package pack.GetAllInstalledApplications;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class GetAllInstalledApplicationsExample extends Activity {

	 public  ArrayList<packageInfoStruct> res = new ArrayList<packageInfoStruct>();
	 public ListView list;
	 public String app_labels[];

	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main);

	        getPackages();

	        list = (ListView)findViewById(R.id.ListView01);
			try{
				list.setAdapter(new ArrayAdapter<string>(this,
					android.R.layout.simple_dropdown_item_1line, app_labels));
			}catch(Exception e){
				System.out.println("Err ++> " + e.getMessage());
				Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
			}

	 }
	private ArrayList<packageInfoStruct> getPackages() {
	    ArrayList<packageInfoStruct> apps = getInstalledApps(false);
	    final int max = apps.size();
	    for (int i=0; i < max; i++) {
	        apps.get(i);
	    }
	    return apps;
	}

	private ArrayList<packageInfoStruct> getInstalledApps(boolean getSysPackages) {

	    List<packageInfo> packs = getPackageManager().getInstalledPackages(0);
	    try{
	    	app_labels = new String[packs.size()];
	    }catch(Exception e){
			Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
		}
	    for(int i=0;i < packs.size();i++) {
	        PackageInfo p = packs.get(i);
	        if ((!getSysPackages) && (p.versionName == null)) {
	            continue ;
	        }
	        PackageInfoStruct newInfo = new PackageInfoStruct();
	        newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
	        newInfo.pname = p.packageName;
	        newInfo.versionName = p.versionName;
	        newInfo.versionCode = p.versionCode;
	        newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
	        res.add(newInfo);

	        app_labels[i] = newInfo.appname;
	    }
	    return res;
	}
}
/* This class is for storing the data for each application */
class PackageInfoStruct {
    String appname = "";
    String pname = "";
    String versionName = "";
    int versionCode = 0;
    Drawable icon;
}

The main.xml file

<?xml version="1.0" encoding="utf-8"?>
<linearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
 <listView android:id="@+id/ListView01"
  android:layout_height="375px"
  android:layout_width="fill_parent">
 </listView>
</linearLayout>

The manifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="pack.GetAllInstalledApplications"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".GetAllInstalledApplicationsExample"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
Get all installed applications in your ANDROID Phone

Showin all applications installed in your ANDROID Phone or emulator

You can download the source code from here.

11 thoughts on “How to get information about all the applications installed in your ANDROID Emulator or Phone? / How to use getPackageManager() in ANDROID ?

  1. ghostged

    Thanks! And whether it is possible a source code of this project?

    Reply
  2. Ekjyot

    this code is not working…
    giving exception Failed to find an AVD compatible with target ‘Google APIs’.

    Reply
    1. James

      Oh Ekjyot, Create an AVD with the Target as Google API in the Android AVD Manager. The AVD you currently using is not for Maps it is the normal Target.

      Reply
      1. Ekjyot

        But now it is giving me the exception Failure starting core services

        Reply
  3. D2

    The above given code dont work . I have spend 5 hrs on this code but all in vain. So my request to others dont waste your time in using this code ….

    Reply
    1. James

      @D2 :- No one has ever any complaints about this code. You might be doing something wrong in your code.

      Reply
      1. D2

        I had copied your code and just pasted it but your code didnt worked and about 5 hrs i worked on this code but all in vain..

        Reply
    1. espoir

      hello,
      I have an Error in ” try{
      list.setAdapter(new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line, app_labels));
      } ”
      Error: The constructor ArrayAdapter(GetAllInstalledApplicationsExample, int, String[]) is undefined
      how can I resolve it?

      Reply
  4. net whiz

    This was a perfect program for what I want but now i am trying to edit it so that it only displays the applications that needs internet permission.Anyone with a sugggestion of what i need to do?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *