zoukankan      html  css  js  c++  java
  • 监控Activity在前后台状态的切换

    
    
    public class BaseActivity extends Activity{


    @Override
    protected void onStop() {
    boolean isOnForeground=isAppOnForeground();
    RaiingLog.d("前后台测试-->>onStop-->>" + isAppOnForeground()+", name-->>"+this.getClass().getName()+", isShowDialog-->>"+isShowDialog);
    super.onStop();
    }

    @Override
    protected void onResume() {
    super.onResume();
    boolean isOnForeground=isAppOnForeground();
    RaiingLog.d("前后台测试-->>onResume-->>" + isAppOnForeground()+", name-->>"+this.getClass().getName()+", isShowDialog-->>"+isShowDialog);

    }
    /**
    * 程序是否在前台运行
    *
    * @return
    */
    public boolean isAppOnForeground() {
    // Returns a list of application processes that are running on the
    // device

    ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
    String packageName = getApplicationContext().getPackageName();

    List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
    .getRunningAppProcesses();
    if (appProcesses == null)
    return false;

    for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
    // The name of the process that this object is associated with.
    if (appProcess.processName.equals(packageName)
    && appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
    return true;
    }
    }

    return false;
    }
    }
  • 相关阅读:
    虚拟机添加新磁盘挂载,导致以前的文件丢失解决办法
    python实现tab键自动补全
    设计模式
    js 深浅拷贝 笔记总结
    js 闭包
    flex 布局
    vue2.0 之 生命周期
    移动端适配 rem
    vue 之 双向绑定原理
    vue2.0 之 过渡动画transtion
  • 原文地址:https://www.cnblogs.com/xiaorenwu702/p/4874876.html
Copyright © 2011-2022 走看看