Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the list of running applications?

Tags:

android

I am working on an app which needs the information of the apps running at the system up to now. Is there an API/method to retrieve that kind of information?

like image 494
Jimmy Avatar asked Jul 21 '10 23:07

Jimmy


3 Answers

You cannot detect an App launch in Android, but you can get the list of currently open apps and check if the app you're looking for is open or not using the following code:

ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();

for (int i = 0; i < runningAppProcessInfo.size(); i++) {
  if(runningAppProcessInfo.get(i).processName.equals("com.the.app.you.are.looking.for")) {
    // Do your stuff here.
  }
}

You can also check if the app is running in the foreground using this method

public static boolean isForeground(Context ctx, String myPackage){
    ActivityManager manager = (ActivityManager) ctx.getSystemService(ACTIVITY_SERVICE);
    List< ActivityManager.RunningTaskInfo > runningTaskInfo = manager.getRunningTasks(1); 

    ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
    if(componentInfo.getPackageName().equals(myPackage)) {
        return true;
    }       
    return false;
}
like image 102
desidigitalnomad Avatar answered Oct 22 '22 11:10

desidigitalnomad


public static String getActiveApps(Context context) {

    PackageManager pm = context.getPackageManager();
    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

    String value = u.dateStamp(); // basic date stamp
    value += "---------------------------------\n";
    value += "Active Apps\n";
    value += "=================================\n";

    for (ApplicationInfo packageInfo : packages) {

        //system apps! get out
        if (!isSTOPPED(packageInfo) && !isSYSTEM(packageInfo)) {

            value += getApplicationLabel(context, packageInfo.packageName) + "\n" + packageInfo.packageName  + "\n-----------------------\n";

        }
    }

    return value;

    //result on my emulator

    /* 2 Ekim 2017 Pazartesi 14:35:17
    ---------------------------------
    Active Apps
    =================================
    SystemSetting
    com.xyz.systemsetting
    -----------------------
    myMail
    com.my.mail
    -----------------------
    X-plore
    com.lonelycatgames.Xplore
    -----------------------
    Renotify
    com.liamlang.renotify
    -----------------------
    Mail Box
    com.mailbox.email
    -----------------------   */


}

some opened apps

isSTOPPED

private static boolean isSTOPPED(ApplicationInfo pkgInfo) {

    return ((pkgInfo.flags & ApplicationInfo.FLAG_STOPPED) != 0);
}

isSYSTEM

private static boolean isSYSTEM(ApplicationInfo pkgInfo) {

    return ((pkgInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
}

getApplicationLabel

public static String getApplicationLabel(Context context, String packageName) {

    PackageManager        packageManager = context.getPackageManager();
    List<ApplicationInfo> packages       = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
    String                label          = null;

    for (int i = 0; i < packages.size(); i++) {

        ApplicationInfo temp = packages.get(i);

        if (temp.packageName.equals(packageName))
            label = packageManager.getApplicationLabel(temp).toString();
    }

    return label;
}
like image 41
hsyn tr Avatar answered Oct 22 '22 09:10

hsyn tr


You can get information about running processes using the ActivityManager class.

like image 9
David Underhill Avatar answered Oct 22 '22 11:10

David Underhill