Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android check if application package is launchable

Tags:

android

launch

I'm looking for the best way to check if application is launchable. There is my code :

PackageManager packageManager = context.getPackageManager();
List<PackageInfo> packs = packageManager.getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
    PackageInfo p = packs.get(i);
    if (packageManager.getLaunchIntentForPackage(p.applicationInfo.packageName) != null) {
        // Get application info
    }
}

This works, but when i do app profiling i noticed that packageManager.getLaunchIntentForPackage() method consumes a lot of execution time, so i'm looking for an alternative way to check if each application is launchable without getting the launch intent.

Any idea ? Thank you !

like image 351
Meher Avatar asked Sep 01 '26 10:09

Meher


1 Answers

I found solution for my own problem, i hope it will help someone :

        PackageManager packageManager = context.getPackageManager();

            Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            final List<ResolveInfo> apps = packageManager.queryIntentActivities(
                    mainIntent, 0);
            Collections.sort(apps, new ResolveInfo.DisplayNameComparator(
                    packageManager));

            for (ResolveInfo resolveInfo : apps) {
            // Get application data here
            }
like image 52
Meher Avatar answered Sep 04 '26 01:09

Meher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!