26 Temmuz 2011 Salı

Get the list of activities/applications installed on Android

Following is the code to get the list of activities/applications installed on Android :
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);

You will get all the necessary data in the ResolveInfo to start a application. You can check ResolveInfo javadoc here.


Here's a cleaner way using the packageManager
final PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages = pm
.getInstalledApplications(PackageManager.GET_META_DATA);

for (ApplicationInfo packageInfo : packages) {

Log.d(TAG, "Installed package :" + packageInfo.packageName);
Log.d(TAG,
"Launch Activity :"
+ pm.getLaunchIntentForPackage(packageInfo.packageName));

}// the getLaunchIntentForPackage returns an intent that you can use with startActivity()
}

More info here http://qtcstation.com/2011/02/how-to-launch-another-app-from-your-app/

To filter on sytem based apps :
private boolean isSystemPackage(ResolveInfo ri){
return ((ri.activityInfo.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)!=0)?true:false;

}



private List<String> getInstalledComponentList() {
Intent componentSearchIntent = new Intent();
componentSearchIntent.addCategory(Constants.COMPONENTS_INTENT_CATEGORY);
componentSearchIntent.setAction(Constants.COMPONENTS_INTENT_ACTION_DEFAULT);
List<ResolveInfo> ril = getPackageManager().queryIntentActivities(componentSearchIntent, PackageManager.MATCH_DEFAULT_ONLY);
List<String> componentList = new ArrayList<String>();
Log.d(LOG_TAG, "Search for installed components found " + ril.size() + " matches.");
for (ResolveInfo ri : ril) {
if (ri.activityInfo != null) {
componentList.add(ri.activityInfo.packageName);// + ri.activityInfo.name);
Log.d(LOG_TAG, "Found installed: " + componentList.get(componentList.size()-1));
}
}
return componentList;
}

Hiç yorum yok:

Yorum Gönder