zoukankan      html  css  js  c++  java
  • 如何知道某个ACTIVITY是否在前台?

    本文链接:http://zengrong.net/post/1680.htm



    有一个Android应用包含包含一个后台程序,该程序会定期连接服务器来实现自定义信息的推送。但是,当这个应用处于前台的时候,后台程序就没有必要连接服务器了。这样可以节省网络资源,也更省电。

    用什么方法知道该应用是否处于前台呢?

    网上搜到的方法大多数都是使用下面的代码:

    但是查阅Android文档后发现,google并不推荐使用这个方法:

    This should never be used for core logic in an application, such as deciding between different behaviors based on the information found here. Such uses are not supported, and will likely break in the future. For example, if multiple applications can be actively running at the same time, assumptions made about the meaning of the data here for purposes of control flow will be incorrect.

    而且,这个方法还要求设置android.permission.GET_TASKS权限。

    因此,我必须寻找更加合适的方法来做这件事。最终,我找到这个方法getRunningAppProcesses(),它并不需要增加特殊的权限。

    下面是范例代码:

  • 相关阅读:
    servlet里面拿到common.property的属性
    js 播放声音文件
    dataGridViewX操作
    CYQ学习主要摘要4
    CYQ学习主要摘要3
    CYQ学习主要摘要2
    CYQ学习主要摘要
    EF操作VS中
    C# 文件与二进制互转数据库写入读出
    简单的线程与界面通用方法,不是很好,但是很方便
  • 原文地址:https://www.cnblogs.com/leihupqrst/p/4054500.html
Copyright © 2011-2022 走看看