Android Question INline java 4 get running package name

Discussion in 'Android Questions' started by ArminKH, Jul 14, 2015.

  1. ArminKH

    ArminKH Well-Known Member

    hi can any body help me to use this code with inline java?
    public class DetectCalendarLaunchRunnable implements Runnable {

    public void run() {
    String[] activePackages;
        activePackages = getActivePackages();
    } else {
        activePackages = getActivePackagesCompat();
      if (activePackages != null) {
        for (String activePackage : activePackages) {
          if (activePackage.equals("")) {
            //Calendar app is launched, do something
      mHandler.postDelayed(this, 1000);

    String[] getActivePackagesCompat() {
      final List<ActivityManager.RunningTaskInfo> taskInfo = mActivityManager.getRunningTasks(1);
      final ComponentName componentName = taskInfo.get(0).topActivity;
      final String[] activePackages = new String[1];
      activePackages[0] = componentName.getPackageName();
      return activePackages;

    String[] getActivePackages() {
      final Set<String> activePackages = new HashSet<String>();
      final List<ActivityManager.RunningAppProcessInfo> processInfos = mActivityManager.getRunningAppProcesses();
      for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
        if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
      return activePackages.toArray(new String[activePackages.size()]);
  2. ArminKH

    ArminKH Well-Known Member

  3. DonManfred

    DonManfred Expert Licensed User

    your posted code will not work on lollipop too.

    Last edited: Jul 15, 2015
    ArminKH and jahswani like this.
  4. jahswani

    jahswani Active Member Licensed User

    Yes @arminkh will not work on lolipop... Try this
    ActivityManager manager = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);

    List<ActivityManager.RunningAppProcessInfo> tasks = manager.getRunningAppProcesses(); 

    Last edited: Jul 15, 2015
    Douglas Farias and ArminKH like this.
  5. ArminKH

    ArminKH Well-Known Member

    Is this java code?
  6. DonManfred

    DonManfred Expert Licensed User

    ArminKH likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice