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?
    Code:
    public class DetectCalendarLaunchRunnable implements Runnable {

    @Override
    public void run() {
      
    String[] activePackages;
      
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
        activePackages = getActivePackages();
      
    } else {
        activePackages = getActivePackagesCompat();
      }
      if (activePackages != null) {
        for (String activePackage : activePackages) {
          if (activePackage.equals("com.google.android.calendar")) {
            //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) {
          activePackages.addAll(Arrays.asList(processInfo.pkgList));
        }
      }
      return activePackages.toArray(new String[activePackages.size()]);
    }
    }
    tnx
     
  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
    Code:
    ActivityManager manager = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);

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

    Log.i("current_app",tasks.get(0).processName);
     
    Last edited: Jul 15, 2015
    Douglas Farias and ArminKH like this.
  5. ArminKH

    ArminKH Well-Known Member

    Is this java code?
    Tnx
     
  6. DonManfred

    DonManfred Expert Licensed User

    yes
     
    ArminKH likes this.
Loading...
  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