Feeds:
Posts
Comments

Archive for the ‘Android Query Package Manager’ Category

In this blog we will discuss how to query the Android Package Manager.

Package Manager Class is used for retrieving various kinds of information related to the application packages that are currently installed on the device. It can be queried to find out what all applications are installed, what are the main Activities for these applications, their permissions etc.

So, let us say if we want to know the name of all the applications installed on our device with their main activity classes name, this can be done as follows.

// get the package manager object
		PackageManager pm = getPackageManager();
		// create an Intent with ACTION_MAIN and CATEGORY LAUNCHER
		Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
		mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

		// query the package manager for the desired intent
		// obtain the list of ResolveInfo objects
		List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
		for (ResolveInfo resolveInfo : list) {

			ActivityInfo activityInfo = resolveInfo.activityInfo;

			// get the name of the Main activity class
			String activityName = activityInfo.name;

			ApplicationInfo applicationInfo = activityInfo.applicationInfo;
			// get the name of the application class
			String applicationName = applicationInfo.className;

			if (applicationName != null) {
				Log.v("APP_NAME", applicationName);

			}

			if (activityName != null) {
				Log.v("ACTIVITY_NAME", activityName);
			}
		}

The ContextWrapper class has a method getPackageManager() which returns the PackageManager Object. On this pacakge manager object we can call the method queryIntentActivities. This method retrieve all activities that can be performed for the given intent.

queryIntentActivities takes 2 argumennts.
Intent– The desired intent as per resolveActivity().
flags– Additional option flags. The most important is MATCH_DEFAULT_ONLY, to limit the resolution to only those activities that support the CATEGORY_DEFAULT.
Returns-This method returns the list of ResolveInfo objects containing one entry for each matching Activity

In our example, we have created an Intent object with ACTION_MAIN and Intent.CATEGORY_LAUNCHER as we are interested in getting a list of all the applications which are listed in the launcher screen.
Once we obtain the list the same is iterated to get the application names and their main activity names.

Advertisements

Read Full Post »