Tutorials Android How to Get List of Installed Apps in Android

How to Get List of Installed Apps in Android

Android PackageManager class is used to retrieve information on the application packages that are currently installed on the device. You can get an instance of PackageManager class by calling getPackageManager(). PackageManager provides methods for querying and manipulating installed packages and related permissions, etc. In this Android example, we we get list of installed apps in Android.

How to Get List of Installed Apps in Android

PackageManager packageManager = getPackageManager();
List<ApplicationInfo> list = packageManager.getInstalledApplications(PackageManager.GET_META_DATA)

packageManager.getInstalledApplications() return a List of all application packages that are installed on the device. If we set the flag GET_UNINSTALLED_PACKAGES has been set, a list of all applications including those deleted with DONT_DELETE_DATA (partially installed apps with data directory) will be returned.

1. Creating application layout in xml

activity_main.xml

As you can see in the attached screenshot, we will be creating a ListView to show all of the installed applications in android.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

snippet_list_row.xml

This layout is being used by the ListView Adapter for representing application details. It shows application icon, application name and application package.

<?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" >

    <ImageView
        android:id="@+id/app_icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:padding="3dp"
        android:scaleType="centerCrop" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:paddingLeft="5dp" >

        <TextView
            android:id="@+id/app_name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/app_paackage"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical" />
    </LinearLayout>

</LinearLayout>

 

2. Writing Java class

AllAppsActivity.java

This is the main application class that is used to initialize and list the installed applications. As getting the list of application details from PackageManage is a long running task, we will do that in AsyncTask. Also, this class is using custom Adapter “ApplicationAdapter” for custom ListView.

package com.javatechig.listapps;

import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class AllAppsActivity extends ListActivity {
	private PackageManager packageManager = null;
	private List<ApplicationInfo> applist = null;
	private ApplicationAdapter listadaptor = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		packageManager = getPackageManager();

		new LoadApplications().execute();
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);

		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		boolean result = true;

		switch (item.getItemId()) {
		case R.id.menu_about: {
			displayAboutDialog();

			break;
		}
		default: {
			result = super.onOptionsItemSelected(item);

			break;
		}
		}

		return result;
	}

	private void displayAboutDialog() {
		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(getString(R.string.about_title));
		builder.setMessage(getString(R.string.about_desc));

		builder.setPositiveButton("Know More", new DialogInterface.OnClickListener() {
		       public void onClick(DialogInterface dialog, int id) {
		    	   Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://stacktips.com"));
		    	   startActivity(browserIntent);
		    	   dialog.cancel();
		       }
		   });
		builder.setNegativeButton("No Thanks!", new DialogInterface.OnClickListener() {
		       public void onClick(DialogInterface dialog, int id) {
		            dialog.cancel();
		       }
		});

		builder.show();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		ApplicationInfo app = applist.get(position);
		try {
			Intent intent = packageManager
					.getLaunchIntentForPackage(app.packageName);

			if (null != intent) {
				startActivity(intent);
			}
		} catch (ActivityNotFoundException e) {
			Toast.makeText(AllAppsActivity.this, e.getMessage(),
					Toast.LENGTH_LONG).show();
		} catch (Exception e) {
			Toast.makeText(AllAppsActivity.this, e.getMessage(),
					Toast.LENGTH_LONG).show();
		}
	}

	private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
		ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
		for (ApplicationInfo info : list) {
			try {
				if (null != packageManager.getLaunchIntentForPackage(info.packageName)) {
					applist.add(info);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return applist;
	}

	private class LoadApplications extends AsyncTask<Void, Void, Void> {
		private ProgressDialog progress = null;

		@Override
		protected Void doInBackground(Void... params) {
			applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
			listadaptor = new ApplicationAdapter(AllAppsActivity.this,
					R.layout.snippet_list_row, applist);

			return null;
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}

		@Override
		protected void onPostExecute(Void result) {
			setListAdapter(listadaptor);
			progress.dismiss();
			super.onPostExecute(result);
		}

		@Override
		protected void onPreExecute() {
			progress = ProgressDialog.show(AllAppsActivity.this, null,
					"Loading application info...");
			super.onPreExecute();
		}

		@Override
		protected void onProgressUpdate(Void... values) {
			super.onProgressUpdate(values);
		}
	}
}

ApplicationAdapter.java

package com.javatechig.listapps;

import java.util.List;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo> {
	private List<ApplicationInfo> appsList = null;
	private Context context;
	private PackageManager packageManager;

	public ApplicationAdapter(Context context, int textViewResourceId,
			List<ApplicationInfo> appsList) {
		super(context, textViewResourceId, appsList);
		this.context = context;
		this.appsList = appsList;
		packageManager = context.getPackageManager();
	}

	@Override
	public int getCount() {
		return ((null != appsList) ? appsList.size() : 0);
	}

	@Override
	public ApplicationInfo getItem(int position) {
		return ((null != appsList) ? appsList.get(position) : null);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (null == view) {
			LayoutInflater layoutInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = layoutInflater.inflate(R.layout.snippet_list_row, null);
		}

		ApplicationInfo applicationInfo = appsList.get(position);
		if (null != applicationInfo) {
			TextView appName = (TextView) view.findViewById(R.id.app_name);
			TextView packageName = (TextView) view.findViewById(R.id.app_paackage);
			ImageView iconview = (ImageView) view.findViewById(R.id.app_icon);

			appName.setText(applicationInfo.loadLabel(packageManager));
			packageName.setText(applicationInfo.packageName);
			iconview.setImageDrawable(applicationInfo.loadIcon(packageManager));
		}
		return view;
	}
};

Download Complete Example

Download complete Eclipse project source code from GitHub.

Total
0
Shares
guest
46 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
anurag
anurag
8 years ago

nice

javatechig
javatechig
7 years ago

nothing you have to change. Just follow the basic steps to create fragments. most/all codes should be reusable.

Nicholas
Nicholas
7 years ago

I just need to display the applications list. do you know if I need all those code. As I don’t want next screen about the information. Just the app list.
appreciate if you can help me out.
Thank you

javatechig
javatechig
7 years ago
Reply to  Nicholas

You probably want to remove onItemClick() and displayAboutDialog() method.

javatechig
javatechig
7 years ago

Please do paste the logcat error. ll help in finding the cause.

pushkar shetye
pushkar shetye
7 years ago

thanks a lot it works.

chetan kotkar
chetan kotkar
7 years ago

how can we list the apps with specific permission like apps with SMS_SEND permission ? can you suggest some idea..

JavaTechig
JavaTechig
7 years ago
Reply to  chetan kotkar

You can use below code snippet to find out the permissions used for each of the application. And write your own logic to filter if the app contains SMS_SEND permission.

PackageInfo packageInfo = pm.getPackageInfo(applicationInfo.packageName, PackageManager.GET_PERMISSIONS);

String[] requestedPermissions = packageInfo.requestedPermissions;

vipul
vipul
6 years ago
Reply to  JavaTechig

Will this work if i have to check whether a particular app uses internet data? I actually want the list of apps using internet data(in background) and an option to stop it from consuming data in my app. Thanks 🙂

JavaTechig
JavaTechig
7 years ago

Sorry. We dont have the example as you looking for.. You have to tweak the example to fit your requirement

JavaTechig
JavaTechig
7 years ago

It will be helpful if you can specify what is not working.

param
param
7 years ago
Reply to  JavaTechig

yeah ok now it working but how to set on off functionality with each app ,means when scrolled on it should be open that app but when it scrolled off i should not open that app

param
param
7 years ago

how to add on off functionality with each installed app which is shown in list view

param
param
7 years ago

how to add on off functionality with each installed app which is shown in list view
as like below snap thanks in advance because i developing school project so please help me

JavaTechig
JavaTechig
7 years ago
Reply to  param

You can design your custom list row with Switch widget.

Similar example is here
http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html

param
param
7 years ago
Reply to  JavaTechig

i already tried with switch but i did not get clear solution

Cao Xuân Phong
Cao Xuân Phong
7 years ago

Perfect. Thanks you.

mike
mike
7 years ago

Hi, is this code open source? Can I used it to create my own application for the Google play store?

Nilanchala Panigrahy
Nilanchala Panigrahy
7 years ago
Reply to  mike

Yes. It is purely open source and you are free to use in your own app.

Natan Rosenfeld
Natan Rosenfeld
6 years ago

I also do not know….

Nilanchala Panigrahy
Nilanchala Panigrahy
6 years ago

This is an error. Thanks for pointing it out.

This suppose to be if (null != applicationInfo) {

Nilanchala Panigrahy
Nilanchala Panigrahy
6 years ago

fixed the same.

Natan Rosenfeld
Natan Rosenfeld
6 years ago

What is the variable data?

Nilanchala Panigrahy
Nilanchala Panigrahy
6 years ago

This is an error. Thanks for pointing it out.

This suppose to be applicationInfo

Sunil Palakurthy
Sunil Palakurthy
6 years ago

Hi Panigrahy,

How can we display the apps in Alphabetical Order. and it is stuttering very much while scrolling, I think it is due to loading of app’s icon, is there any way to reduce the stutter?

Thanks in advance. 🙂

Nilanchala Panigrahy
Nilanchala Panigrahy
6 years ago

You need to cache the app icons yourself. Before that just have a check if the ViewHolder pattern solves this problem.

Cicklow
Cicklow
6 years ago

Thanks for the code, it has served me well, I needed to make an application that can uninstall android applications 🙂
I added the option to sort the list, uninstall, see information, run. And thank you for the code 🙂

pham nghia
pham nghia
6 years ago
Reply to  Cicklow

Can u give me code about show permissions of each app ? tks so much

Harmeet
Harmeet
6 years ago

hello sir how can i get this list of installed application in tab view

Busuulwa Charles
Busuulwa Charles
6 years ago

how can i uninstall any app from that list

Busuulwa Charles
Busuulwa Charles
6 years ago

Hey, thanks a lot. all is well

Roney
Roney
6 years ago

hello Brother i want to develop an app which monitors when an installed app has started running and stopped then find the duration and store them in database

Hardy
Hardy
6 years ago

Hi

How can I get Application Adapter class to loadLable (app Name) string in main class.

shekhar shrivastava
shekhar shrivastava
6 years ago

hello ,
how can i stop any particular app from getting open

Andries Van Zyl
Andries Van Zyl
6 years ago

Hi there This has helped allot but i would like to save all the apps with their Logos in a database and then display them is the list view. how can i do that? Thanx in advance.

Aakarsh Rai
Aakarsh Rai
6 years ago

can i get the list of uninstalled application form my device

Prachi
Prachi
6 years ago

How can I get the list of currently running apps in my phone?

Suryakiran Molleti
Suryakiran Molleti
5 years ago

How to get AndroidManifest.xml file for all the apps?

Nilanchala Panigrahy
Nilanchala Panigrahy
5 years ago

All information can be retrieved form ApplicationInfo class

Suryakiran Molleti
Suryakiran Molleti
5 years ago
john
john
5 years ago

Hi bro, iam newbie in android so can u help me..:'(, can we get the name of each apk then compare it with the name in databse?how to to that?please do help me and interesting to know the answer for this question above “how can we list the apps with specific permission like apps with SMS_SEND permission ? can you suggest some idea..” 🙂

John Tegalombo
John Tegalombo
5 years ago

Thanks for your Tutorial,

how to add a button to disable / enable app in the list?

Emre Uğur
Emre Uğur
5 years ago

How can i stop installed app notifications that i want with code? I wanna stop them in list view

Utkarsh Sharma
Utkarsh Sharma
2 years ago

how can i get the list of apps which are not used for long time? and also how to uninstall the apps from the list

Abhishek Kamal
Abhishek Kamal
2 years ago

Thanks, Bro it’s working like a Magic.

morsalin hosan
morsalin hosan
2 years ago

if i want to add hide installed app icons system on this project what i need to do?

Previous Post
Starting an activity for a result

Starting an Android Activity for Result

Next Post
Android Tutorials

How to Turn off, Turn on Wifi in Android

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
46
0
Would love your thoughts, please comment.x
()
x