0 Flares Twitter 0 Facebook 0 Google+ 0 0 Flares ×

Android PackageManager class is used for retrieving various kinds of information related to the application packages that are currently installed on the device. You can get an instance of this class through getPackageManager().

Project Description

In this Android 4 example, we will get list of installed apps in Android device and create custom ListView and populate its items using custom BaseAdapter.

Here, we are going to implement OnItemClickListener event listener which calls onItemClick() callback method where we retrieve a particular row and create a object containing android application information and start another activity to display the installed app information such as Application name, package name, version, features, required permissions, path info, target SDK version, installed and modified date.

Environment Used

JDK 6 (Java SE 6)

Eclipse Indigo/Juno IDE for Java EE Developers

Android SDK 4.0.3 / 4.2 Jelly Bean

Android Development Tools (ADT) Plugin for Eclipse (ADT version 21.0.0)

Refer this link to setup the Android development environment or this link to update to a latest version of Android SDK

Prerequisites

Create Android Project

Create a new Android Project and enter the Application name as AppsList .

as . Project name as AppsList .

. Enter the package name as com.ibc.android.demo.appslist.activity .

as . Enter the Activity name as ApkListActivity .

as . Enter the Layout name as main .

as . Click Finish.

strings.xml

Open res/values/strings.xml and replace it with following content.

<resources> <string name="app_name">AppsList</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_apk_list">AppList Demo</string> <string name="appicon">Application Icon</string> <string name="apkname">Application Name:</string> <string name="package_name">Package Name:</string> <string name="version">Version:</string> <string name="features">Required Features:</string> <string name="permissions">Required Permissions:</string> <string name="req_version">Taget SDK Version:</string> <string name="path_info">Path Info:</string> <string name="appsize">Physical Size</string> <string name="installed">Installed:</string> <string name="modified">Last Modified:</string> </resources>

XML layout files

main layout file (main.xml)

This file defines a layout for displaying the result of installed apps in ListView widget. Open main.xml file in res/layout and copy the following content.

<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" > <ListView android:id="@+id/applist" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>

ListView row item layout file (apklist_item)

<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/appname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:paddingTop="5dp" android:paddingLeft="10dp" android:paddingBottom="5dp" android:textStyle="bold" />

This layout defines only TextView widget. We can display of the icon of installed app in TextView using setCompoundDrawables() method.

Second Activity’s layout file (apkinfo.xml)

<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/apk" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="18dp" android:layout_marginTop="24dp" android:text="@string/apkname" android:textStyle="bold" /> <TextView android:id="@+id/applabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/apk" android:paddingLeft="24dp" /> <TextView android:id="@+id/pack_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/applabel" android:layout_marginLeft="18dp" android:layout_marginTop="15dp" android:text="@string/package_name" android:textStyle="bold" /> <TextView android:id="@+id/package_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/pack_name" android:paddingLeft="24dp" /> <TextView android:id="@+id/version" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/package_name" android:layout_marginTop="15dp" android:layout_marginLeft="18dp" android:text="@string/version" android:textStyle="bold" /> <TextView android:id="@+id/version_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/version" android:paddingLeft="24dp" /> <TextView android:id="@+id/features" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/version_name" android:layout_marginTop="15dp" android:layout_marginLeft="18dp" android:text="@string/features" android:textStyle="bold" /> <TextView android:id="@+id/req_feature" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/features" android:paddingLeft="24dp" /> <TextView android:id="@+id/permissions" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/req_feature" android:layout_marginTop="15dp" android:layout_marginLeft="18dp" android:text="@string/permissions" android:textStyle="bold" /> <TextView android:id="@+id/req_permission" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/permissions" android:paddingLeft="24dp" /> <TextView android:id="@+id/path_info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/req_permission" android:layout_marginTop="15dp" android:layout_marginLeft="18dp" android:text="@string/path_info" android:textStyle="bold" /> <TextView android:id="@+id/path" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/path_info" android:paddingLeft="24dp" /> <TextView android:id="@+id/and_version" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/path" android:layout_marginTop="15dp" android:layout_marginLeft="18dp" android:text="@string/req_version" android:textStyle="bold" /> <TextView android:id="@+id/andversion" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/and_version" android:paddingLeft="24dp" /> <TextView android:id="@+id/installed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/andversion" android:layout_marginTop="15dp" android:layout_marginLeft="18dp" android:text="@string/installed" android:textStyle="bold" /> <TextView android:id="@+id/insdate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/installed" android:paddingLeft="24dp" /> <TextView android:id="@+id/modified" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/insdate" android:layout_marginTop="15dp" android:layout_marginLeft="18dp" android:text="@string/modified" android:textStyle="bold" /> <TextView android:id="@+id/last_modify" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/modified" android:paddingLeft="24dp" /> </RelativeLayout> </ScrollView>

Create AppData class

This class extends android.app.Application which is used to maintain global application state, in this case contains an instance variable android.content.pm.PackageInfo. PackageInfo contains overall information about the contents of a package. This corresponds to all of the information collected from AndroidManifest.xml.

Create a new Java class “AppData.java” in package “com.ibc.android.demo.appslist.app” and copy the following code.

package com.ibc.android.demo.appslist.app; import android.app.Application; import android.content.pm.PackageInfo; public class AppData extends Application { PackageInfo packageInfo; public PackageInfo getPackageInfo() { return packageInfo; } public void setPackageInfo(PackageInfo packageInfo) { this.packageInfo = packageInfo; } }

Custom BaseAdapter class

Create a new class “ApkAdapter” in “com.ibc.android.demo.appslist.adapter” package and copy the following code. This class extends android.widget.BaseAdapter to provide custom row layout and data for ListView.

package com.ibc.android.demo.appslist.adapter; import java.util.List; import com.ibc.android.demo.appslist.activity.R; import android.app.Activity; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.graphics.drawable.Drawable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class ApkAdapter extends BaseAdapter { List<PackageInfo> packageList; Activity context; PackageManager packageManager; public ApkAdapter(Activity context, List<PackageInfo> packageList, PackageManager packageManager) { super(); this.context = context; this.packageList = packageList; this.packageManager = packageManager; } private class ViewHolder { TextView apkName; } public int getCount() { return packageList.size(); } public Object getItem(int position) { return packageList.get(position); } public long getItemId(int position) { return 0; } public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; LayoutInflater inflater = context.getLayoutInflater(); if (convertView == null) { convertView = inflater.inflate(R.layout.apklist_item, null); holder = new ViewHolder(); holder.apkName = (TextView) convertView.findViewById(R.id.appname); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } PackageInfo packageInfo = (PackageInfo) getItem(position); Drawable appIcon = packageManager .getApplicationIcon(packageInfo.applicationInfo); String appName = packageManager.getApplicationLabel( packageInfo.applicationInfo).toString(); appIcon.setBounds(0, 0, 40, 40); holder.apkName.setCompoundDrawables(appIcon, null, null, null); holder.apkName.setCompoundDrawablePadding(15); holder.apkName.setText(appName); return convertView; } }

Activity class

MainActivity class (ApkListActivity.java)

Open this activity class and copy the following code. In onCreate(), we get the list of installed apps and filtering out system app.

package com.ibc.android.demo.appslist.activity; import java.util.List; import com.ibc.android.demo.appslist.adapter.ApkAdapter; import com.ibc.android.demo.appslist.app.AppData; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; public class ApkListActivity extends Activity implements OnItemClickListener { PackageManager packageManager; ListView apkList; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); packageManager = getPackageManager(); List<PackageInfo> packageList = packageManager .getInstalledPackages(PackageManager.GET_PERMISSIONS); List<PackageInfo> packageList1 = new ArrayList<PackageInfo>(); /*To filter out System apps*/ for(PackageInfo pi : packageList) { boolean b = isSystemPackage(pi); if(!b) { packageList1.add(pi); } } apkList = (ListView) findViewById(R.id.applist); apkList.setAdapter(new ApkAdapter(this, packageList1, packageManager)); apkList.setOnItemClickListener(this); } /** * Return whether the given PackgeInfo represents a system package or not. * User-installed packages (Market or otherwise) should not be denoted as * system packages. * * @param pkgInfo * @return boolean */ private boolean isSystemPackage(PackageInfo pkgInfo) { return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true : false; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long row) { PackageInfo packageInfo = (PackageInfo) parent .getItemAtPosition(position); AppData appData = (AppData) getApplicationContext(); appData.setPackageInfo(packageInfo); Intent appInfo = new Intent(getApplicationContext(), ApkInfo.class); startActivity(appInfo); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_apk_list, menu); return true; } }

Second Activity class (ApkInfo.java)

Create a new class “ApkInfo.java” in package “com.ibc.android.demo.appslist.activity” and copy the following code.

package com.ibc.android.demo.appslist.activity; import java.text.SimpleDateFormat; import java.util.Date; import com.ibc.android.demo.appslist.app.AppData; import android.annotation.SuppressLint; import android.app.Activity; import android.content.pm.FeatureInfo; import android.content.pm.PackageInfo; import android.os.Bundle; import android.widget.TextView; public class ApkInfo extends Activity { TextView appLabel, packageName, version, features; TextView permissions, andVersion, installed, lastModify, path; PackageInfo packageInfo; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.apkinfo); findViewsById(); AppData appData = (AppData) getApplicationContext(); packageInfo = appData.getPackageInfo(); setValues(); } private void findViewsById() { appLabel = (TextView) findViewById(R.id.applabel); packageName = (TextView) findViewById(R.id.package_name); version = (TextView) findViewById(R.id.version_name); features = (TextView) findViewById(R.id.req_feature); permissions = (TextView) findViewById(R.id.req_permission); andVersion = (TextView) findViewById(R.id.andversion); path = (TextView) findViewById(R.id.path); installed = (TextView) findViewById(R.id.insdate); lastModify = (TextView) findViewById(R.id.last_modify); } private void setValues() { // APP name appLabel.setText(getPackageManager().getApplicationLabel( packageInfo.applicationInfo)); // package name packageName.setText(packageInfo.packageName); // version name version.setText(packageInfo.versionName); // target version andVersion.setText(Integer .toString(packageInfo.applicationInfo.targetSdkVersion)); // path path.setText(packageInfo.applicationInfo.sourceDir); // first installation installed.setText(setDateFormat(packageInfo.firstInstallTime)); // last modified lastModify.setText(setDateFormat(packageInfo.lastUpdateTime)); // features if (packageInfo.reqFeatures != null) features.setText(getFeatures(packageInfo.reqFeatures)); else features.setText("-"); // uses-permission if (packageInfo.requestedPermissions != null) permissions .setText(getPermissions(packageInfo.requestedPermissions)); else permissions.setText("-"); } @SuppressLint("SimpleDateFormat") private String setDateFormat(long time) { Date date = new Date(time); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm"); String strDate = formatter.format(date); return strDate; } // Convert string array to comma separated string private String getPermissions(String[] requestedPermissions) { String permission = ""; for (int i = 0; i < requestedPermissions.length; i++) { permission = permission + requestedPermissions[i] + ",

"; } return permission; } // Convert string array to comma separated string private String getFeatures(FeatureInfo[] reqFeatures) { String features = ""; for (int i = 0; i < reqFeatures.length; i++) { features = features + reqFeatures[i] + ",

"; } return features; } }

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ibc.android.demo.appslist.activity" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="17" /> <application android:name="com.ibc.android.demo.appslist.app.AppData" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.ibc.android.demo.appslist.activity.ApkListActivity" android:label="@string/title_activity_apk_list" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ApkInfo" android:label="@string/title_activity_apk_list" > </activity> </application> </manifest>

Output

Run your application

First Screen/Activity



Second Screen/Activity



Project Folder Structure

The complete folder structure of this example is shown below.

