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
How to Implement a Long Click Listener on a Listview
Google Sign in Not Working After Publishing in Play Store
Ideal Way to Set Global Uncaught Exception Handler in Android
Recyclerview Header and Footer
Pass Data from Activity to Service Using an Intent
Turn Off Autosuggest for Edittext
Appcompat V7 R21 Returning Error in Values.Xml
How to Handle an Asynctask During Screen Rotation
How to Put Multiple Project_Number/Sender Id in Google-Services.JSON
The Completest Cocos2D-X Tutorial & Guide List
How to Automatically Update Application on Android
How to Set Custom Header in Volley Request
How to Set Different Label for Launcher Rather Than Activity Title
How to Implement Drawerarrowtoggle from Android Appcompat V7 21 Library
Recyclerview Store/Restore State Between Activities