项目遇到一个轮训,有通知就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. matlab生成多组多维高斯分布数据
  2. 测试基于2SK241的150kHz的导航信号高频放大器
  3. 怎么让项目断开svn连接服务器,SVN断开与服务器连接
  4. Linux入门:部署JavaWeb项目
  5. 建议收藏!百度不到的硬核资源~
  6. border-边框的形状
  7. 4字节 经纬度_java 获取本机经纬度
  8. Hbase的伪分布式安装
  9. Jmeter之线程组(默认)
  10. Python 远程开关机
  11. @工程师,怎样才能让面试者一眼相中你?
  12. Canvas 输出位图
  13. 思科防火墙ASA5520做NAT映射配置实例
  14. 随笔-Python批量调整图片大小
  15. 项目选题报告答辩总结
  16. 中文版的优动漫PAINT与日版CSP有什么不同?
  17. vsftpd基于mysql_vsftpd基于mysql实现用户认证
  18. C语言库函数:memcmp/strcmp和strncmp的区别
  19. 大数据可视化管理antV使用详解
  20. matlab着色问题,着色问题matlab

热门文章

  1. iPhone 游戏 Dungeon Hunter2 地牢猎手 今日通关
  2. Oracle将小数转换为百分数;及将小于1的数字to_char()转成字符串后,个位0丢失的解决办法
  3. 读毛姆《人性的枷锁》
  4. 出主意:阻止了对方倒苦水(错误行为)
  5. Unity WebGL的Autodesk Interactive渲染问题
  6. word插入mathtype公式行间距变大怎么办?
  7. 阿里云SDK和SpringBoot maven项目中jar包冲突的解决办法
  8. 老农解决猫狗鱼的问题(黑马基础习题)
  9. 需求定律公式和需求弹性推导——《可以量化的经济学》
  10. oracle rac 火星舱_火星舱备份一体机基本介绍