如何知道某个ACTIVITY是否在前台?
本文链接:http://zengrong.net/post/1680.htm
有一个Android应用包含包含一个后台程序,该程序会定期连接服务器来实现自定义信息的推送。但是,当这个应用处于前台的时候,后台程序就没有必要连接服务器了。这样可以节省网络资源,也更省电。
用什么方法知道该应用是否处于前台呢?
网上搜到的方法大多数都是使用下面的代码:
|
1
2
3
4
5
6
|
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
//获得task列表
List<ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
Log.d("topActivity", "CURRENT Activity ::"+ taskInfo.get(0).topActivity.getClassName());
ComponentName componentInfo = taskInfo.get(0).topActivity;
componentInfo.getPackageName();
|
但是查阅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(),它并不需要增加特殊的权限。
下面是范例代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/**
* 返回当前的应用是否处于前台显示状态
* @param $packageName
* @return
*/
private boolean isTopActivity(String $packageName)
{
//_context是一个保存的上下文
ActivityManager __am = (ActivityManager) _context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> __list = __am.getRunningAppProcesses();
if(__list.size() == 0) return false;
for(ActivityManager.RunningAppProcessInfo __process:__list)
{
Log.d(getTAG(),Integer.toString(__process.importance));
Log.d(getTAG(),__process.processName);
if(__process.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND &&
__process.processName.equals($packageName))
{
return true;
}
}
return false;
}
|
如何知道某个ACTIVITY是否在前台?的更多相关文章
- Bringing the activity to foreground 将activity切换到前台
今天遇到这个问题,找了很久,网上一些解决方法不够完全.特做此记录: 经测试以下方法不能将在后台运行的activity切换到前台运行! Intent i = new Intent(); i.setCla ...
- Android 面试题--Activity
1.什么是 Activity?Activity是Android组件中最基本也是最为常见用的四大组件(Activity,Service服务,Content Provider内容提供,BroadcastR ...
- Xamarin android 之Activity详解
序言: 上篇大概的讲解了新建一个android的流程.今天为大家带来的是Activity详解,因为自己在开发过程中就遇到 好几次坑,尴尬. 生命周期 和Java里头一样一样的,如图 图片来源于网上哈, ...
- Android Activity生命周期
从android api文档摘抄出来的activity生命周期图如下: Activity有如下四种状态 a.活动状态 activity处于屏幕前台,获取到了焦点可以和用户进行交互,同一时刻只有一个a ...
- Android Activity的生命周期简单总结
Android Activity的生命周期简单总结 这里的内容参考官方的文档,这篇文章的目的不是去总结Activity是如何启动,如何创造,以及暂停和销毁的,而是从实际开发中分析在Activity各个 ...
- Android Activity 启动模式和任务栈
在了解了基本的Activity的生命周期后,我们能够很好的在一个Activity上面做相关的业务.但是这是不够的,因为Android通过任务栈来保存整个APP的Activity,合理的调度任务栈才能够 ...
- Android之Activity的生命周期
PS:写一发关于Activity的生命周期,也算是面试的重点内容. 学习内容: 1.Activity的生命周期 2.面对多种情况的时候Activity的生命周期 3.onSaveInstanceSta ...
- Android Activity生命周期详讲
管理 Activity 生命周期 通过实现回调方法管理 Activity 的生命周期对开发强大而又灵活的应用至关重要. Activity 的生命周期会直接受到 Activity 与其他 Activit ...
- android之Activity回传数据
约定:当Activity发生跳转时将原来的Activity成为父Activity,将新出现的Activity成为子Activity. 情景设置 下面是个发短信的Activity 当我们点击图中的+按钮 ...
随机推荐
- Stream:java1.8新特性
原 Stream:java1.8新特性 2017年08月01日 18:15:43 kekeair-zhang 阅读数:392 标签: streamjava1-8新特性 更多 个人分类: 日记 版权声明 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 网格系统实例:手机、平板电脑、台式电脑
<!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 手机.平板电脑.台式电脑</title&g ...
- Spring AOP 中 advice 的四种类型 before after throwing advice around
spring AOP(Aspect-oriented programming) 是用于切面编程,简单的来说:AOP相当于一个拦截器,去拦截一些处理,例如:当一个方法执行的时候,Spring 能够拦截 ...
- LVS负载均衡软件使用及(LVS简介、三种工作模式、十种调度算法)
一.LVS简介 LVS(Linux Virtual Server)即Linux虚拟服务器,目前LVS已经被集成到Linux内核模块中.该项目在Linux内核中实现了基于IP的数据请求负载均衡调度方案, ...
- 解决CentOS7用yum安装软件显示错误:cannot find a valid baseurl for repo: base/7/x86_64
使用yun安装软件时有时会报repo文件的错误,, 主要问题出自于CentOS-Base.repo文件 解决方案:将这个文件后缀名修改使这个文件无效 [root@localhost ~]# cd /e ...
- 研究Zookeeper的原理(二)
阅读声明:以下内容是结合网上材料及工作内容所写的个人理解,如有不当,欢迎大家指正~~~谢谢啦 一.ZooKeeper的选举机制.FailOver机制 我们知道ZooKeeper在分布式环境中协调服务, ...
- Vagrant 安装使用
先安装虚拟机 https://www.virtualbox.org/ 再安装 https://www.vagrantup.com/ 1.nginxhttp://nginx.org/download/ ...
- 关于java自学的内容以及感受(7.21)
直接切入正题说一下自学到的内容: 定义合法标识符的规则: 可以由英文字母,数字,_,$组成. 不能数字开头和包含空格. 不可以使用关键字和保留字,但是可以包含关键字和保留字. byte short i ...
- python面试题手动总结答案锦集
数据类型 字符串 1.列举python中的基本数据类型 数字:int 布尔值:bool 字符串:str 列表:list 元组:tuple 字典:dict 集合:set 然后我们需要了解一些运算符,应为 ...
- Lucene学习笔记一
Lucene课件 1.全文检索 1.1常见的全文检索 在window系统中,可以指定磁盘中的某一个位置来搜索你想要得到的东西.这个功能是windows比较常用的功能.在这个界面中能搜索的内容有*.*, ...