Discovering if Android activity is running

社会主义新天地 提交于 2019-11-28 10:20:22

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

Easiest way to check that whether an Activity is running or not is:

Context context = MyActivity.this; 

if (! ((Activity) context).isFinishing()) {
    //  Activity is running
} else {
    //  Activity has been finished
}

Note: If activity is not running you should not perform any UI related operation.

Shankar Agarwal

Copied from here.

you can use a static variable within the activity.

class MyActivity extends Activity {
     static boolean active = false;

      public void onStart() {
         active = true;
      } 

      public void onStop() {
         active = false;
      }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!