I want to work on an app similar to Package Name Viewer 2.0 that could load the packagenames of all the installed apps in the device. Actually I have searched a lot about it even discussed with my seniors and even tried to contact with the owner of Package Name Viewer 2.0but got no answer. I am not asking to write the whole code just give a favor that how can i achieve this.
In short: I want to add the code in onCreate() that whenever app starts it should load all the packagenames of installed apps.
Related Question: How to get package name from anywhere?
Use PackageManager to retrieve ApplicationInfo of all apps.
public static List<App> getAllAppInstalled(Context context) {
PackageManager packageManager = context.getPackageManager();
List<App> apps = new ArrayList<>();
List<ApplicationInfo> packages = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
String applicationName = ((packageInfo != null) ? packageManager.getApplicationLabel(packageInfo) : "Unknown").toString();
if (packageInfo.enabled) {
apps.add(new App(applicationName, packageInfo.packageName));
}
}
return apps;
}
App class should look like:
public class App {
private String appName;
private String appPackage;
public App(String appName, String appPackage) {
this.appName = appName;
this.appPackage = appPackage;
}
//getters and setters
}
I am writing a code to extract the package name, application name, and icon from the last installed app on my phone. I can get the application common name and icon from the application info, but I can't seem to figure out how to get the package name. All the codes that I have found to get the package name give me the package name of MY app, not the last installed app.
It seems like I need to find a method to get the package name, where I can pass in the application info as the parameter (like I do for the application common name and icon).
final PackageManager pm = context.getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo(intent.getData().getSchemeSpecificPart(), 0);
Log.d("tag_name","Application Info" + ai);
PACKAGE_NAME = context.getApplicationContext().getPackageName();
Log.d("tag_name","Package Name" + PACKAGE_NAME);
} catch (final PackageManager.NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
Log.d("tag_name", "Application NAME" + applicationName);
// http://www.carbonrider.com/2016/01/01/extract-app-icon-in-android/
try {
Drawable icon = context.getPackageManager().getApplicationIcon(ai);
Log.d("tag_name", "ICON" + icon);}
catch (Exception e){}
Firstly receive all apps with this Code :
List<PackageInfo> packages = packageManager.getInstalledPackages(PackageManager.GET_META_DATA);
Then sort the packages list with this code :
Collections.sort(packages, new Comparator<PackageInfo>() {
#Override
public int compare(PackageInfo p1, PackageInfo p2) {
return Long.toString(p2.firstInstallTime).compareTo(Long.toString(p1.firstInstallTime));
}
});
Then you can receive the package Name of the latest installed app this way:
packages.get(0).packageName
I am trying to read AndroidManifest file to extract all android permissions in eclipse plugin developnebt but have reached a dead end. It is not able to open file in /Project_root_directory/res/AndroidManifest.xml. I searched and found that it is in encrypted form.
I can't figure how to read it using Java. Any help is appreciated.
private void getInstalledApps()
{
PackageManager pkgMgr = c.getPackageManager();
List<ApplicationInfo> list = pkgMgr.getInstalledApplications(0);
for (ApplicationInfo content : list)
{
String pname = content.packageName;
String appLabel = (String) pkgMgr.getApplicationLabel(content);
PackageInfo pi = null;
try
{
pi = pkgMgr.getPackageInfo( pname,
PackageManager.GET_META_DATA |
PackageManager.GET_PERMISSIONS );
String[] reqPerms = pi.requestedPermissions;
}
catch ( NameNotFoundException e1 )
{
if (D) e1.printStackTrace();
}
}
}
In my manifest I have:
<application
android:name=".MyApp"
android:icon="#drawable/ic_launcher_icon"
android:label="#string/app_name"
android:debuggable="true">
How do I get the label element?
Note: My code is running inside of someone else's, so I don't have access to #string/app_name
There's an easier way than the other answers that doesn't require you to name the resource explicitly or worry about exceptions with package names. It also works if you have used a string directly instead of a resource.
Just do:
public static String getApplicationName(Context context) {
ApplicationInfo applicationInfo = context.getApplicationInfo();
int stringId = applicationInfo.labelRes;
return stringId == 0 ? applicationInfo.nonLocalizedLabel.toString() : context.getString(stringId);
}
Edit
In light of the comment from Snicolas, I've modified the above so that it doesn't try to resolve the id if it is 0. Instead it uses, nonLocalizedLabel as a backoff. No need for wrapping in try/catch.
If not mentioned in the strings.xml/hardcoded in AndroidManifest.xml for whatever reason like android:label="MyApp"
Java
public String getAppLable(Context context) {
ApplicationInfo applicationInfo = null;
try {
applicationInfo = context.packageManager.getApplicationInfo(context.getPackageManager().getApplicationInfo().packageName, 0);
} catch (final NameNotFoundException e) {
Log.d("TAG", "The package with the given name cannot be found on the system.");
}
return (applicationInfo != null ? packageManager.getApplicationLabel(applicationInfo) : "Unknown");
}
Or if you know the String resource ID then you can directly get it via
getString(R.string.appNameID);
UPDATE
Kotlin
fun getAppLable(context: Context): String? {
var applicationInfo: ApplicationInfo? = null
try {
applicationInfo = context.packageManager.getApplicationInfo(context.applicationInfo.packageName, 0)
} catch (e: NameNotFoundException) {
Log.d("TAG", "The package with the given name cannot be found on the system.")
}
return (if (applicationInfo != null) packageManager.getApplicationLabel(applicationInfo) else "Unknown")
}
Java
public static String getApplicationName(Context context) {
return context.getApplicationInfo().loadLabel(context.getPackageManager()).toString();
}
Kotlin (as extension)
fun Context.getAppName(): String = applicationInfo.loadLabel(packageManager).toString()
From any Context use:
getApplicationInfo().loadLabel(getPackageManager()).toString();
In Kotlin its simple:
val appLabel = context.applicationInfo.nonLocalizedLabel.toString()
In Kotlin, use the following codes to get Application Name:
// Get App Name
var appName: String = ""
val applicationInfo = this.getApplicationInfo()
val stringId = applicationInfo.labelRes
if (stringId == 0) {
appName = applicationInfo.nonLocalizedLabel.toString()
}
else {
appName = this.getString(stringId)
}
If you need only the application name, not the package name, then just write this code.
String app_name = packageInfo.applicationInfo.loadLabel(getPackageManager()).toString();
You can use this
JAVA
ApplicationInfo appInfo = getApplicationContext().getApplicationInfo();
String applicationLabel = getApplicationContext().getPackageManager().getApplicationLabel(appInfo).toString();
Get Appliction Name Using RunningAppProcessInfo as:
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while(i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
Log.w("LABEL", c.toString());
}catch(Exception e) {
//Name Not FOund Exception
}
}
By default you have a string resource called "app_name" generated by AndroidStudio. Why not simply use that? Or any other string resource created for this purpose. Much easier than calling several internal methods to come up with a value you have to set yourself in the first place.
Okay guys another sleek option is
Application.Context.ApplicationInfo.NonLocalizedLabel
verified for hard coded android label on application element.
<application android:label="Big App"></application>
Reference:
http://developer.android.com/reference/android/content/pm/PackageItemInfo.html#nonLocalizedLabel
The source comment added to NonLocalizedLabel directs us now to:
return context.getPackageManager().getApplicationLabelFormatted(context.getApplicationInfo());
Kotlin
A simple function to get the name of the application in kotlin
fun getApplicationName() : String{
var applicationName = ""
try {
val applicationInfo = application.applicationInfo
val stringId = applicationInfo.labelRes
if (stringId == 0) {
applicationName = applicationInfo.nonLocalizedLabel.toString()
}
else {
applicationName = application.getString(stringId)
}
} catch (e: Exception) {
e.printStackTrace()
}
return applicationName
}
Have you tried using the PackageManager#getActivityInfo() method? There will be a field that should contain the name of the application.
See the answer to a very similar question here.
If "don't have access" means you don't get the expected value... Try This:
String appName = getString(R.string.app_name);
I want to get the name of my application. How can i get that?
Thanks in advance.
You can use PackageItemInfo -> nonLocalizedLabel to get application name.
val applicationName = context.applicationInfo.nonLocalizedLabel.toString()
[Old Answer]
Usually, we do add the application name with app_name string resource, so you can write below code to access the app name
String applicationName = getResources().getString(R.string.app_name);
Reference : Resource Types
But note that, this resource name is not mandatory and can be changed to any other string name. In that case, the code will not work. See the first code snippet which uses PackageItemInfo -> nonLocalizedLabel above for a better solution.
You can use PackageManager class to obtain ApplicationInfo:
final PackageManager pm = context.getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo(packageName, 0);
} catch (final NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
EDIT: CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName,PackageManager.GET_META_DATA));
This would return the application name as defined in <application> tag of its manifest.
you can use PackageManager#getApplicationInfo()
For getting the Application Name for all packages installed in the device.
Assuming you have your current Context object ctx
Resources appR = ctx.getResources();
CharSequence txt = appR.getText(appR.getIdentifier("app_name",
"string", ctx.getPackageName()));
Context has function getString(int resId): Context
so you can use it easily like this.
context.getString(R.string.app_name);