关于intent之android.intent.action.USER_PRESENT的接收与使用
发布日期:2021-05-07 19:05:55 浏览次数:20 分类:原创文章

本文共 1099 字,大约阅读时间需要 3 分钟。


在做解锁监听程序时,一开始采用监听屏幕SCREEN_ON和SCREEN_OFF这两个action。


但奇怪的是,这两个action只能通过代码动态的形式注册,才能被监听到,使用AndroidManifest.xml 完全监听不到。


百度后发现这是PowerManager那边在发这个广播的时候做了限制,限制只能有register到代码中的receiver才能接收。




后来就找各种能静态注册
AndroidManifest.xml同时能反映用户解锁行为的广播.于是找到
android.intent.action.USER_PRESENT.


每个用户隔一段时间重新开始使用手机时,首先按电源键
点亮
屏幕,紧接着解锁。
android.intent.action.USER_PRESENT就是
解锁时发出的intent.


于是,监听android.intent.action.USER_PRESENT就能识别用户进入home界面,进而启动想启动的相关服务,包括弹出对话框welcome用户\后台启动程序升级服务等等。




AndroidManifest.xml文件中注册代码

























1 <receiver android:name=".ActionReceiver">










2    <intent-filter android:priority="90000">










3    <action android:name="android.intent.action.USER_PRESENT" />










4    </intent-filter>










5 </receiver>












这个intent的说明文档是:


public static final String ACTION_USER_PRESENT




Since: API Level 3




Broadcast Action: Sent when the user is present after device wakes up (e.g when the keyguard is gone).




This is a protected intent that can only be sent by the system.




Constant Value: "android.intent.action.USER_PRESENT"






翻译过来就是:


注意这个action只能有系统发出,是在用户唤醒机器的时候才会发出这种action.






另外, 真的屏幕解锁的时候触发这个广播,而我们接收这个广播会受好多条件的制约。
比如有些第三方桌面不会真正的锁屏,360,LBE等安全软件对广播的拦截等等.

上一篇:XTabLayout
下一篇:常用的android studio 的快捷键设置和代码块缩写

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2025年03月28日 03时08分02秒