How to get Running Applications package name in android programatically

88 views Asked by At

there,

i need to fetch PackageName of applications that currently running in my device, after that i filter that application that i need, how can i fetch all packages name of the applications that currently running in android?

i need to target some spacefic package name

Like: Whatsapp, Instagram

Note: We are beyond any restriction

I tried this all thinges

in Manifest

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
<uses-permission android:name="android.permission.GET_TASKS"/>
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"
    tools:ignore="ProtectedPermissions" />
    
<queries>
    <package android:name="com.whatsapp" />
</queries>

Sample 1:

try {
            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
            for (ActivityManager.RunningTaskInfo task : tasks) {
                assert task.baseActivity != null;
                if (packageName.equalsIgnoreCase(task.baseActivity.getPackageName()))
                    return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

Sample 2

final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        final List<ActivityManager.RunningAppProcessInfo> processInformation = activityManager.getRunningAppProcesses();
        if (processInformation != null) {
            for (final ActivityManager.RunningAppProcessInfo processInfo : processInformation) {
                Log.e(TAG, "checkAppInForeground: " + processInfo.processName );
                if (processInfo.processName.equals(packageName)) {
                    return true;
                }
            }
        }
        return false;
1

There are 1 answers

1
General Tony On

Check wheather you have added this permission request in manifest, if not add them

uses-permission android:name="android.permission.GET_TASKS"