项目遇到一个轮训,有通知就toast。但是toast是可以不受actvity控制的,当我按home键让app常驻后台后,我要他不吐司,这时候我们需要判断app在前台还是后台。

发挥谷歌搜索的威力 Checking if an Android application is running in the background (或者简单的几个关键字)

binggo–>http://stackoverflow.com/questions/3667022/checking-if-an-android-application-is-running-in-the-background

做程序员就是要学会从stackoverflow抄代码

高票回答是集成在application两个static 方法,然后每个activity继承。当然未尝不可,但是这个代码量增加了。这是疑问难道谷歌没有自带的判断方法吗,当然有。第二个回答更加中肯

ActivityLifecycleCallbacks

The key is using ActivityLifecycleCallbacks (note that this requires Android API level 14 (Android 4.0)). Just check if the number of stopped activities is equal to the number of started activities. If they’re equal, your application is being backgrounded. If there are more started activities, your application is still visible. If there are more resumed than paused activities, your application is not only visible, but it’s also in the foreground. There are 3 main states that your activity can be in, then: visible and in the foreground, visible but not in the foreground, and not visible and not in the foreground (i.e. in the background).

The really nice thing about this method is that it doesn’t have the asynchronous issues getRunningTasks() does, but you also don’t have to modify every Activity in your application to set/unset something in onResumed()/onPaused(). It’s just a few lines of code that’s self contained, and it works throughout your whole application. Plus, there are no funky permissions required either.

此方法在4.0上有效,现在的手机应该没4.0以下的了吧(这么反人类?)。

public class MyLifecycleHandler implements ActivityLifecycleCallbacks {public static MyLifecycleHandler getInstance()   {if (INSTANCE == null)INSTANCE = new MyLifecycleHandler();return INSTANCE;}// I use four separate variables here. You can, of course, just use two and// increment/decrement them instead of using four and incrementing them all.private int resumed;private int paused;private int started;private int stopped;@Overridepublic void onActivityCreated(Activity activity, Bundle savedInstanceState) {}@Overridepublic void onActivityDestroyed(Activity activity) {}@Overridepublic void onActivityResumed(Activity activity) {++resumed;}@Overridepublic void onActivityPaused(Activity activity) {++paused;android.util.Log.w("test", "application is in foreground: " + (resumed > paused));}@Overridepublic void onActivitySaveInstanceState(Activity activity, Bundle outState) {}@Overridepublic void onActivityStarted(Activity activity) {++started;}@Overridepublic void onActivityStopped(Activity activity) {++stopped;android.util.Log.w("test", "application is visible: " + (started > stopped));}// If you want a static function you can use to check if your application is// foreground/background, you can use the following:// Replace the four variables above with these fourprivate static int resumed;private static int paused;private static int started;private static int stopped;// And these two public static functionspublic static boolean isApplicationVisible() {return started > stopped;}public static boolean isApplicationInForeground() {return resumed > paused;}}

有兴趣的可以详细了解actvity的生命周期。4个静态变量

 private static int resumed;private static int paused;private static int started;private static int stopped;

通过静态方法判断

public static boolean    isApplicationVisible() {return started > stopped;}

当然啦这个方法实现就是在app注册一次

// Don't forget to add it to your manifest by doing
// <application android:name="your.package.MyApplication" ...
public class MyApplication extends Application {@Overridepublic void onCreate() {// Simply add the handler, and that's it! No need to add any code// to every activity. Everything is contained in MyLifecycleHandler// with just a few lines of code. Now *that's* nice.registerActivityLifecycleCallbacks(MyLifecycleHandler.getInstance());}
}

四级英语都可以轻松理解了。

在我们需要吐司的时候加个判断,例如

 public void showFast(CharSequence text) {if         (MyLifecycleHandler.getInstance().isApplicationVisible())show(text, 1000);}

最后大功告成。轻松利用静态方法就可以判断app位于前台后台。。

如何判断app在前台还是后台相关推荐

  1. Android 监听APP进入前台、后台

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/117988239 本文出自[赵彦军的博客] 文章目录 方案一:利用ActivityL ...

  2. 如何判断ABAP程序前台还是后台运行[sy-batch]

    可以通过系统变量sy-batch判断abap程序是前台运行还是后台运行,sy-batch等于'X'是后台运行,等于空就是前台运行.

  3. Android判断App前台运行还是后台运行(运行状态)

    原文:http://p.codekk.com/detail/Android/wenmingvs/AndroidProcess AndroidProcess 项目地址:https://github.co ...

  4. 前台和后台是要写两个工程吗_如何判断一个Bug属于前台还是后台

    这里的前台和后台也叫前端和后端.前台基本是能在页面上可看得见的错误,而后台是看不到的,如UI界面样式相关的错误不用判断肯定是前台的,用户数据问题基本是后台的. 前台一般的工作是获取.加载.计算.渲染数 ...

  5. 新闻APP前台和后台管理系统 MVP+Dragger2+RxJava+RetroFit

    一 系统说明 该新闻资讯APP 主要是用户可以查看各种各样的新闻资讯,并且可以进行注册,登录账号,评论,收藏,取消收藏,浏览新闻,发布新闻,修改修改,删除新闻,用户管理,个人信息等操作,该APP分为前 ...

  6. android5.0以后获取应用运行状态,Android判断App前台运行还是后台运行(运行状态)...

    本文通过图文并茂的方式给大家介绍android判断app状态的相关内容,具体详情如下所示: 要了解这块,首先需要明白一些概念,app,process,task 1.process就是进程,是linux ...

  7. 安卓判断APP是在前台还是在后台

    安卓中判断APP是否在前台: 方法一:CCApplication 中判断 private boolean mIsInForeground = false; public boolean isInFor ...

  8. Android 判断app是否在前台还是在后台运行

    Android 判断app是否在前台还是在后台运行,直接看代码,可直接使用. [java]  view plain copy public static boolean isBackground(Co ...

  9. 判断App整体处于前台还是后台

    转载请注明转自:[noyet12的博客](http://blog.csdn.net/u012975705) 博客原址:http://blog.csdn.net/u012975705/article/d ...

最新文章

  1. 苹果Swift语言中文教程资源汇总
  2. Codeforces 768E:Game of Stones
  3. Vue绑定数据v-bind缩写:字段名 双向绑定v-model缩写:model 监听动作v-on缩写@ 记住:与数据相关用冒号 与动作相关用@
  4. 浏览器记住密码之后,input背景变黄
  5. 项目 接入 在线预览
  6. 如何查看 MySQL 数据库的引擎
  7. 三阶段DEA模型操作步骤笔记
  8. word如何打出取整符号
  9. 华为修改优先级命令_华为交换机优先级配置
  10. pdf工具类 (pd4ml)
  11. 爬取QQ空间说说日志、好友个人信息并进行加密
  12. Microsoft Edge浏览器重新打开主页没有变化但会重新打开一个淘宝天猫页面的解决办法
  13. 脸书隐藏了未能阻止滥用技术的官僚主义报道的失败
  14. 批流融合系统-SparkV2/Beam
  15. 国内手机验证码短信平台哪个好用?关键在于这4个标准!
  16. 手机静音状态下也让播出声音
  17. stream 计算一个List对象中某个字段总和
  18. nginx小知识 :通过nginx代理转发接口地址
  19. 量子计算更适用于优化:专访首家量子计算上市公司IonQ创始人 克里斯·门罗
  20. 一名开源拓荒者的 Apache 之旅

热门文章

  1. dac0832三角波c语言程序,单片机控制DAC0832输出正弦波三角波汇编程序
  2. 子之错父之过什么意思_生活|为什么子不教,父之过,这是什么意思?
  3. 交通灯控制系统设计左转倒计时protues仿真的c语言程序,交通灯控制系统设计
  4. 习惯养成计划申明(重要)
  5. android+智能家居控制系统,基于Android的智能家居控制系统设计与实现
  6. 192.168.1.1是什么?192.168.1.1详细解释!
  7. “源”来是你-Vol.32 | 知名图数据平台 Neo4j 招聘中国社区经理
  8. Network Slimming
  9. Android UI绘制流程分析(三)measure
  10. 北京邮电大学计算机学院考研夏令营,北京邮电大学理学院2021年保研夏令营活动通知...