Android: how do I check if activity is running?

2019-01-02 17:12发布

Is there any simple way of determining whether or not a certain activity is active? I want to do certain things depending on which activity is active. eg:

if(activityrunning == activity1)
//do this
else if (activityrunning == activity2)
//do something else

20条回答
骚的不知所云
2楼-- · 2019-01-02 17:53

This is code for checking whether a particular service is running. I'm fairly sure it can work for an activity too as long as you change getRunningServices with getRunningAppProcesses() or getRunningTasks(). Have a look here http://developer.android.com/reference/android/app/ActivityManager.html#getRunningAppProcesses()

Change Constants.PACKAGE and Constants.BACKGROUND_SERVICE_CLASS accordingly

    public static boolean isServiceRunning(Context context) {

    Log.i(TAG, "Checking if service is running");

    ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);

    List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);

    boolean isServiceFound = false;

    for (int i = 0; i < services.size(); i++) {

        if (Constants.PACKAGE.equals(services.get(i).service.getPackageName())){

            if (Constants.BACKGROUND_SERVICE_CLASS.equals(services.get(i).service.getClassName())){
                isServiceFound = true;
            }
        }
    }

    Log.i(TAG, "Service was" + (isServiceFound ? "" : " not") + " running");

    return isServiceFound;

}
查看更多
明月照影归
3楼-- · 2019-01-02 17:53

if(!activity.isFinishing() && !activity.isDestroyed())

查看更多
呛了眼睛熬了心
4楼-- · 2019-01-02 17:53

Use the isActivity variable to check if activity is alive or not.

private boolean activityState = true;

 @Override
protected void onDestroy() {
    super.onDestroy();
    activityState = false;
}

Then check

if(activityState){
//add your code
}
查看更多
宁负流年不负卿
5楼-- · 2019-01-02 17:56

In addition to the accepted answer, if you have multiple instances of the activity, you can use a counter instead:

class MyActivity extends Activity {

     static int activeInstances = 0;

     static boolean isActive() {
        return (activeInstance > 0)
     }

      @Override
      public void onStart() {
         super.onStart();
         activeInstances++;
      } 

      @Override
      public void onStop() {
         super.onStop();
         activeInstances--;
      }
}
查看更多
牵手、夕阳
6楼-- · 2019-01-02 17:57

This work if you don't have the same activity in foreground. If you open from notification don't work i made some adjustments and came with this:

public static boolean ativo = false;
public static int counter = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    counter++;
}

@Override
protected void onStart() {
    super.onStart();
    ativo = true;
}

@Override
protected void onStop() {
    super.onStop();
    if (counter==1) ativo = false;
}

@Override
protected void onDestroy() {
    counter--;
    super.onDestroy();
}

That works for me with several activitys open at the same time.

查看更多
萌妹纸的霸气范
7楼-- · 2019-01-02 17:58

I used a check if (!a.isFinishing()) and it seems to do what i need. a is the activity instance. Is this incorrect? Why didn't anyone try this?

查看更多
登录 后发表回答