Discovering if Android activity is running

Solution 1: You can use ActivityManager for Checking if Activity is Running or not:

public boolean isActivityRunning() { 

ActivityManager activityManager = (ActivityManager)Monitor.this.getSystemService (Context.ACTIVITY_SERVICE); 
    List<RunningTaskInfo> activitys = activityManager.getRunningTasks(Integer.MAX_VALUE); 
    isActivityFound = false; 
    for (int i = 0; i < activitys.size(); i++) { 
        if (activitys.get(i).topActivity.toString().equalsIgnoreCase("ComponentInfo{com.example.testapp/com.example.testapp.Your_Activity_Name}")) {
            isActivityFound = true;
        }
    } 
    return isActivityFound; 
} 

need to add the permission to your manifest..

<uses-permission  android:name="android.permission.GET_TASKS"/>

Solution 2: Your can use an static variable in your activity for which you want to check it's running or not and store it some where for access from your service or broadcast receiver as:

static boolean CurrentlyRunning= false;
      public void onStart() {
         CurrentlyRunning= true; //Store status of Activity somewhere like in shared //preference 
      } 
      public void onStop() {
         CurrentlyRunning= false;//Store status of Activity somewhere like in shared //preference 
      }

I hope this was helpful!


The next approach would work well if you want to handle incoming Google Cloud message (C2DM) by your activity (if any is running) or issue a notification if no activities are running.

Register one BroadcastReceiver in the manifest file. This receiver will handle C2D messages whenever application not running. Register another BroadcastReceiver programmatically in your activity. This receiver will handle C2D messages whenever activity is running.

AndoroidManifest.xml

<receiver
    android:name=".StaticReceiver"
    android:permission="com.google.android.c2dm.permission.SEND" >
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <category android:name="com.mypackage" />
    </intent-filter>
</receiver>

MyReceiver.java

public class StaticReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Trigger a Notification
    }
}

MyActivity.java

public class MyActivity extends ActionBarActivity {

    @Override
    protected void onResume() {
    super.onResume();

        final IntentFilter filter = new 
                IntentFilter("com.google.android.c2dm.intent.RECEIVE");
        filter.addCategory("com.mypackage");
        filter.setPriority(1); 
        registerReceiver(dynamicReceiver, filter, 
                "com.google.android.c2dm.permission.SEND", null);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(dynamicReceiver);
    }

    private final BroadcastReceiver dynamicReceiver 
            = new BroadcastReceiver() 
    {
        @Override
        public void onReceive(Context context, Intent intent) {

            // TODO Handle C2DM

            // blocks passing broadcast to StaticReceiver instance
            abortBroadcast();
       }
    };
}

Note! To catch broadcasts first, the priority of dynamicReceiver IntentFilter must be higher than priority of StaticReceiver instance IntentFilter (default priority is '0').

PS. It looks like broadcasts issued by Google Cloud Messaging Service are ordered broadcasts. Original idea author: CommonsWare