Reputation: 41
I want to get a list of app names installed on android phone. However, when I run my code, I can't get some of the app names correctly as shown below.
D/applicationName: com.android.gallery3d.app.GalleryActivity
D/applicationName: Messaging
D/applicationName: Settings
D/applicationName: Maps
D/applicationName: com.android.documentsui.LauncherActivity
D/applicationName: res/menu/example_menu.xml
D/applicationName: res/color/abc_primary_text_material_light.xml
Also, when the correct app name cannot be obtained, the following error occurs
E/mple.getapplis: Resource 7f100082 is a complex map type.
W/PackageManager: Failure retrieving text 0x7f100082 in package com.android.documentsui
android.content.res.Resources$NotFoundException: String resource ID #0x7f100082
at android.content.res.Resources.getText(Resources.java:444)
at android.app.ApplicationPackageManager.getText(ApplicationPackageManager.java:2004)
at android.content.pm.ComponentInfo.loadUnsafeLabel(ComponentInfo.java:103)
at android.content.pm.PackageItemInfo.loadLabel(PackageItemInfo.java:210)
at android.content.pm.ResolveInfo.loadLabel(ResolveInfo.java:225)
at com.example.getapplist.MainActivity.onCreate(MainActivity.kt:22)
at android.app.Activity.performCreate(Activity.java:7994)
at android.app.Activity.performCreate(Activity.java:7978)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Sometimes I get a list of each app name is correct, but after a few builds, the above situation occurs. And this situation occurs only if my code runs in api 30 and above. My code is below.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val pm = this.packageManager
val intent = Intent()
intent.action = Intent.ACTION_MAIN
intent.addCategory(Intent.CATEGORY_LAUNCHER)
val applications = pm.queryIntentActivities(intent, 0)
for (resolverInfo in applications) {
val applicationName = resolverInfo.loadLabel(pm).toString()
Log.d("applicationName", applicationName)
}
}
}
I already added following setting in AndroidManifest.xml
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
Does anyone know how to fix it?
Upvotes: 2
Views: 374
Reputation: 41
I removed my app and rebuilt, this problem was solved! I think this is a bug of IDE.
Upvotes: 2
Reputation: 302
try this
public String getAppLable(Context context) {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo applicationInfo = null;
try {
applicationInfo = packageManager.getApplicationInfo(context.getApplicationInfo().packageName, 0);
} catch (final NameNotFoundException e) {
}
return (String) (applicationInfo != null ? packageManager.getApplicationLabel(applicationInfo) : "Unknown");
}
Upvotes: 0