Getting a Package Name from an Application Label (Display Name)

Standard

This is the first of several posts I’ve begun writing about an open source machine learning utility app for Android. Stay tuned!

I’ve been looking around for a way to launch apps using their display names (officially, the “Application Label”). It seems like a pretty straightforward thing to want to do, and I see plenty of apps that do it, but for some reason, everyone is playing their cards close to their chest (which is annoying, and part of why I wrote this post). So without further ado, a simple method that takes an application label (perhaps from user input?) and launches the desired app:

PackageManager pm = context.getPackageManager();
List<ApplicationInfo> l = pm.getInstalledApplications(PackageManager.GET_META_DATA);
String canonicalName = “”;
for (ApplicationInfo ai : l){
String n = (String)pm.getApplicationLabel(ai);
if (n.contains(name) || name.contains(n)){
canonicalName = ai.packageName;
}
}

//do whatever you want with canonicalName, e.g. launch the app,
Intent app = context.getPackageManager().getLaunchIntentForPackage(canonicalName);
context.startActivity(app);

And there you have it! Share, enjoy, comment.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s