• Android开发检测App从后台进入前台的解决方法


    public class ActLifecycle implements ActivityLifecycleCallbacks {
    
      private int mPreActivityStartedCount = 0;
      private int mActivityStartedCount = 0;
      private int mActivityOrientation = Configuration.ORIENTATION_UNDEFINED;
    
      @Override
      public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
      }
    
      @Override
      public void onActivityStarted(Activity activity) {
        mPreActivityStartedCount = mActivityStartedCount;
        mActivityStartedCount++;
        int orientation = getOrientation(activity);
        if (mPreActivityStartedCount == 0 && mActivityStartedCount == 1
            && orientation == mActivityOrientation) {
          handleAppForeground(activity);//app从后台进入前台了,在这个自定义的方法里处理具体的业务
        }
        mActivityOrientation = orientation;
      }
    
      private int getOrientation(Activity activity) {
        if (activity != null && activity.getResources() != null) {
          Configuration configuration = activity.getResources().getConfiguration();
          if (configuration != null) {
            return configuration.orientation;
          }
        }
        return Configuration.ORIENTATION_UNDEFINED;
      }
    
      @Override
      public void onActivityResumed(Activity activity) {
      }
    
      @Override
      public void onActivityPaused(Activity activity) {
      }
    
      @Override
      public void onActivityStopped(Activity activity) {
        mPreActivityStartedCount = mActivityStartedCount;
        mActivityStartedCount--;
      }
    
      @Override
      public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
      }
    
      @Override
      public void onActivityDestroyed(Activity activity) {
      }
    }

    //注册app生命周期回调函数
    application.registerActivityLifecycleCallbacks(new ActLifecycle());
     
  • 相关阅读:
    【poj1008】Maya Calendar
    【CodeVS4093】EZ的间谍网络
    2016.6.19 模拟考试
    【poj1129】Channel Allocation
    【poj2676】Sudoku
    Java 2D API
    Java 2D API
    IntelliJ IDEA
    IntelliJ IDEA 使用说明(For Eclipse user)
    IntelliJ IDEA
  • 原文地址:https://www.cnblogs.com/yongfengnice/p/10986310.html
Copyright © 2020-2023  润新知