は私のAppクラスに次のメソッドでそれを行うために管理:
LifecycleHandlerが
public class LifecycleHandler implements Application.ActivityLifecycleCallbacks {
private static LifecycleHandler instance;
public static synchronized LifecycleHandler getInstance() {
if (instance == null) {
instance = new LifecycleHandler();
}
return instance;
}
private int resumed;
private int paused;
private int started;
private int stopped;
private WeakReference<Activity> activityRef;
public Activity getCurrentActivity() {
if (activityRef != null) {
return activityRef.get();
}
return null;
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
++resumed;
//Logging.log(TAG, "onActivityResumed application is in foreground: " + (resumed > paused));
if (started > stopped && resumed > paused) {
}
activityRef = new WeakReference<>(activity);
}
@Override
public void onActivityPaused(Activity activity) {
++paused;
//Logging.log(TAG, "onActivityPaused application is in foreground: " + (resumed > paused));
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityStarted(Activity activity) {
++started;
//Logging.log(TAG, "onActivityStarted application is visible: " + (started > stopped));
}
@Override
public void onActivityStopped(Activity activity) {
++stopped;
//Logging.log(TAG, "onActivityStopped application is visible: " + (started > stopped));
if (isInBackground()) {
}
}
public boolean isInBackground() {
return started <= stopped && resumed <= paused;
}
}
そして、私たちは人生を登録する必要がある
public static Point getSize(){
Point point = new Point();
if (Build.VERSION.SDK_INT >= 21 && LifecycleHandler.getInstance().getCurrentActivity() != null){
DisplayMetrics metrics = new DisplayMetrics();
LifecycleHandler.getInstance().getCurrentActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
point.x = metrics.widthPixels;
point.y = metrics.heightPixels;
} else {
((WindowManager) appContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getSize(point);
}
return point;
}
アプリケーションクラスのサイクル:
registerActivityLifecycleCallbacks(LifecycleHandler.getInstance());