android判断app置于后台监听home键
来源:互联网 发布:好看的小说言情 知乎 编辑:程序博客网 时间:2024/06/11 20:23
from:http://blog.sina.com.cn/s/blog_474928c90100yrnh.html
背景:
用户按下Home键将程序置于后台运行或者应用启动其他activity,比如系统浏览器,短信等,需要向系统发送通知,用户做完别的操作后,点击通知栏,回到应用。问题一
在什么时机发送通知?
用户按下Home的事件在应用层时捕捉不到的,因此只能从activity生命周期方法着手。
方法一:
系统所有activity继承一个BaseActivity,在BaseActivity中维护一个当前可见的activity数组:
protected static ArrayList
在onResume中,将当前activity保存,同时清除所有通知:
protected void onResume()
{
if (!sVisibleActivities.contains(this))
{
sVisibleActivities.add(this);
}
// 清除系统消息
mNotificationManager.cancel(R.id.notify);
}
在onStop中,清除保存的当前activity:
protected void onStop()
{
if (sVisibleActivities.contains(this))
{
sVisibleActivities.remove(this);
}
// 如果当前没有可见的activity,则发送系统通知
if (sVisibleActivities.isEmpty())
{
sendBackgroundNotify();
}
super.onStop();
}
这种方式在大多数情况下工作良好,可以达到需求,但是问题时,当前台的activity被至于后台时,onStop()方法不一定会被调用,因此通知有可能不会被发出!
方法二:
几经周折,发现activity有一个生命周期方法可以达到目的:
protected void onUserLeaveHint ()
Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint()
will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint()
will not be called on the activity being interrupted. In cases when it is invoked, this method is called right before the activity's onPause()
callback.
This callback and onUserInteraction()
are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notfication.
See Also
onUserInteraction()
public void onUserInteraction ()
Called whenever a key, touch, or trackball event is dispatched to the activity. Implement this method if you wish to know that the user has interacted with the device in some way while your activity is running. This callback and onUserLeaveHint()
are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notfication.
All calls to your activity's onUserLeaveHint()
callback will be accompanied by calls to onUserInteraction()
. This ensures that your activity will be told of relevant user activity such as pulling down the notification pane and touching an item there.
Note that this callback will be invoked for the touch down action that begins a touch gesture, but may not be invoked for the touch-moved and touch-up actions that follow.
See Also
onUserLeaveHint()
从文档来看,这个方法似乎就是为了按下Home键时这样的场景设计的。
这样,在onUserLeaveHint里发出系统通知即可。
但是问题又来了,如果启动应用,从一个activity依次调用startActivity,finish关闭自己,启动一个新的activity时,onUserLeaveHint也会被调用....
再次翻阅文档,发现Intent中的一个Flag:
Since:
If
Typically,
The
If
这正是我想要的,这样,在启动activity时,往intent中加上这个flag,onUserLeaveHint就不会再被调用了,hoory...
- android判断app置于后台监听home键
- 监听android home键后台运行
- home键监听(Android应用切换到后台监听)
- android 判断切换后台 判断按下home键
- android 判断切换后台 判断按下home键
- Android系统开放App程序监听Home键的权限
- android监听home键
- android 监听HOME键
- Android 监听Home键
- android Home键监听
- Android 监听Home键
- Android 监听home键
- android HOME键监听
- android home键监听
- Android Home键监听
- android监听home键
- android监听Home键
- Android Home键监听
- MAC上使用apktool进行反编译
- arm中R0-R15寄存器的作用
- Excel两列数据比较,相同的保存在另外一列
- Highcharts X轴文字竖排
- Loader异步装载
- android判断app置于后台监听home键
- 表连接-全连接
- Java之transient
- Git简单使用教程(三)-创建版本库
- Linux 静态库与共享库
- JM和x264是什么关系?
- 【VC小项目】-14.1排序函数模板
- Substring with Concatenation of All Words
- mysql分布式集群实现原理