Junior
How to get a list of all applications installed on the phone?
sobes.tech AI
Answer from AI
// Get the package manager
val packageManager: PackageManager = context.packageManager
// Define flags for additional information (optional)
// In this case - only basic app info
val flags = PackageManager.GET_META_DATA
// Get a list of all installed packages (applications)
val applications: List<ApplicationInfo> = packageManager.getInstalledApplications(flags)
// Example of printing package names
for (app in applications) {
// Get the package name (sometimes used as ID)
val packageName = app.packageName
// Optionally, get a readable app name
val appName = packageManager.getApplicationLabel(app).toString()
println("Package Name: $packageName, App Name: $appName")
}
You need to add the QUERY_ALL_PACKAGES permission to the manifest file if the target API level is above 30 and you need to get a list of all applications, not just those your app interacts with.
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>