I’m working on an Android app (screen time, kotlin) where I need to display the names of the apps along with their usage statistics. To achieve this, I’ve implemented a getAppName function in my code to retrieve the app names using their package names. However, I’ve noticed that for some apps, the function only returns the package name instead of their actual names.
private fun getAppName(packageName: String): String {
return try {
val pm: PackageManager = context?.packageManager!!
val applicationInfo = pm.getApplicationInfo(packageName, 0)
pm.getApplicationLabel(applicationInfo).toString()
} catch (e: PackageManager.NameNotFoundException) {
packageName // Fallback to package name if app name is not found
}
}
And here’s how I’m using it:
val appName = getAppName(usageStat.packageName)
For some apps(Gallery,settings,camera), this works fine and I’m able to display their names correctly. However, for most of the apps, it only returns their package names.
Is there something wrong with my implementation of getAppName? Or is there a limitation or special case that I’m not aware of? I would appreciate any insights or suggestions on how to ensure that I can consistently retrieve the names of all apps.
Thank you in advance for your help!
Chua Jun Yu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1