Get Application Name from Package Name

How to get Application name from package name in android?

You can get Application name from package using following code

final PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo( "your_package_name", 0);
} catch (final NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");

Get app name from package name in android

You need to use PackageManager to get detailed information about installed packages.

PackageManager packageManager = context.getPackageManager();
ApplicationInfo applicationInfo = null;
try {
applicationInfo = packageManager.getApplicationInfo(packageName, 0);
} catch (final NameNotFoundException e) {}
final String title = (String)((applicationInfo != null) ? packageManager.getApplicationLabel(applicationInfo) : "???");

How to get Application Name Using Package Name(Assuming i already have the package name)

put package name to this function and get AppName (App Label)

 public String appName(String pack){
String Name = null;

try{
PackageManager packManager = context.getPackageManager();
ApplicationInfo app = context.getPackageManager().getApplicationInfo(pack, 0);
Name = packManager.getApplicationLabel(app).toString();
}
catch(Exception e){
e.printStackTrace();
}

return Name;
}

How to get package name from anywhere?

An idea is to have a static variable in your main activity, instantiated to be the package name. Then just reference that variable.

You will have to initialize it in the main activity's onCreate() method:

Global to the class:

public static String PACKAGE_NAME;

Then..

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

PACKAGE_NAME = getApplicationContext().getPackageName();
}

You can then access it via Main.PACKAGE_NAME.

Getting the Package name using an App name

PackageManager can be used for the same.

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; // retrieve package name here
}
}


Related Topics



Leave a reply



Submit