title: Android background service to determine foreground application
body: <p>I'm working on an application that will monitor the phone usage throughout the day. To do this I have a background service that starts on device boot and keeps on polling to find out what the current foreground application is.  The following code works when I am just clicking an application then backing out of it and clicking another application.  Now lets say I open the browser and go to a different webpage, it will stop registering the browser as being the foreground application.  Or for example, I open up google talk, it will register it as the current activity, but when I send a message it will no longer register it as the foreground application. Any thoughts or ideas as to what might be the cause of this bug?  If needed I'll provide any additional info, just let me know.
  Maybe the answer lies in this post and I just can't find it... <a href="https://stackoverflow.com/questions/2166961/determining-the-current-foreground-application-from-a-background-task-or-service">Determining the current foreground application from a background task or service</a></p>
  
  <pre><code>public static RunningAppProcessInfo getForegroundApp() throws NameNotFoundException{
      RunningAppProcessInfo result = null, info = null;
      String currApp = null;
      am = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
      Drawable icon = null;
  List &lt;RunningAppProcessInfo&gt; l = am.getRunningAppProcesses();
  Iterator &lt;RunningAppProcessInfo&gt; i = l.iterator();
  PackageManager pm = context.getPackageManager();
  
  while(i.hasNext()){
      info = i.next();
          if(info.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND &amp;&amp; getActivityForApp(info)!=null ){
  
              try {
                  currApp = getCurrentApplication(info);
                  System.out.println(currApp);
                  System.out.println(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
                  icon = pm.getApplicationIcon(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
                  System.out.println(getActivityForApp(info));
              } catch (NameNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
              }
          }
      break;       
  }
  
  return result;
  </code></pre>
  
  <p>}</p>
  
  <pre><code>private static ComponentName getActivityForApp(RunningAppProcessInfo target){
      ComponentName result=null;
      ActivityManager.RunningTaskInfo info;
  
      if(target==null)
          return null;
  
      if(am==null)
          am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
      List &lt;ActivityManager.RunningTaskInfo&gt; l = am.getRunningTasks(9999);
      Iterator &lt;ActivityManager.RunningTaskInfo&gt; i = l.iterator();
  
      while(i.hasNext()){
          info=i.next();
          if(info.baseActivity.getPackageName().equals(target.processName)){
              result=info.topActivity;
              break;
          }
      }
      return result;
  }
  </code></pre>