网上有许多方法,实现了各自的需求。如
1.Android How to Check if the App is in Background or Foreground
2.Is my Android app currently foreground or background?
3.Determining if your Android Application is in Background or Foreground
本方法参考3,但不是用ActivityLifecycleCallbacks,具体如下面:
public class BaseActivity extends Activity {
private final String tag = getClass().getSimpleName();
private static int mResumed;
private static int mStopped;
@Override
protected void onResume() {
super.onResume();
++mResumed;
}
@Override
protected void onStop() {
super.onStop();
++mStopped;
Log.d(tag, "Is in background==" + (mResumed == mStopped));
}
}