How to get information about all the applications installed in your ANDROID Emulator or Phone? / How to use getPackageManager() in ANDROID ?
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>
You can download the source code from here.
Link to this post!
