Find Running activities from a Broadcast Receiver in Android
<p>I want to find from within my broadcast receiver what other activities are currently running. This is the code that i use from an activity to find the other running activites but when i try to use this code in my broadcast receiver i get errors on the following lines:</p>

<pre><code>ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
</code></pre>

<p>shows the error in eclipse ACTIVITY_SERVICE cannot be resolved to a variable</p>

<pre><code> PackageManager pm = this.getPackageManager();
</code></pre>

<p>and this shows the error in eclipse The method getPackageManager() is undefined for the type ScreenReceiver (my broadcast receiver)</p>

<p>Here is the full code:</p>

<pre><code>public void getRunning(){
        ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
        List l = am.getRunningAppProcesses();
        Iterator i = l.iterator();
        PackageManager pm = this.getPackageManager();
        while(i.hasNext()) {
          ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
          try {
            CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
            Log.w("LABEL", c.toString());
            runningApplications.add(c.toString());
          }catch(Exception e) {
            //Name Not Found Exception
          }
        }

    }
</code></pre>
