ACTION_USER_PRESENT、ACTION_SCREEN_ON、ACTION_BOOT_COMPLETED 的广播接收器
我正在创建一个使用广播接收器的类。我想接收有关手机解锁的广播。但有一些问题。请帮帮我。
我的 Manifest.xml 是:-
<receiver android:name=".MyReciever">
<intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_USER_PRESENT" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_SCREEN_ON" />
</intent-filter>
</intent-filter>
</receiver>
和我的广播接收器类:-
public class MyReiever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("My Reciever","is intent null => " + (intent == null));
Log.d("My Reciever",intent.getAction()+"");
}
}
尽管其他应用程序和服务正在接收“Screen_on”和“USer_Present”的广播,例如。无线网络服务。
I am creating a class which uses broadcast receiver. I want to receive the broadcast on unlocking of the phone. But there is some issue. Please help me out.
My Manifest.xml is :-
<receiver android:name=".MyReciever">
<intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_USER_PRESENT" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_SCREEN_ON" />
</intent-filter>
</intent-filter>
</receiver>
and my Broadcast reciever class :-
public class MyReiever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("My Reciever","is intent null => " + (intent == null));
Log.d("My Reciever",intent.getAction()+"");
}
}
Though other application and services are receiving broadcast for "Screen_on" and "USer_Present" eg. WifiService.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尽管 Java 常量为
android.content.intent.ACTION_USER_PRESENT
、android.content.intent.ACTION_BOOT_COMPLETED
和android.content.intent.ACTION_SCREEN_ON
,这些常量的值是android.intent.action.USER_PRESENT
,android.intent.action.BOOT_COMPLETED
和android.intent.action.SCREEN_ON
。这些值需要出现在您的清单中。但请注意,
ACTION_SCREEN_ON
的接收器不能在清单中声明,而必须通过 Java 代码注册,请参阅例如 这个问题。Although the Java constants are
android.content.intent.ACTION_USER_PRESENT
,android.content.intent.ACTION_BOOT_COMPLETED
, andandroid.content.intent.ACTION_SCREEN_ON
, the values of those constants areandroid.intent.action.USER_PRESENT
,android.intent.action.BOOT_COMPLETED
, andandroid.intent.action.SCREEN_ON
. It is those values which need to appear in your manifest.Note, however, that a receiver for
ACTION_SCREEN_ON
can not be declared in a manifest but must be registered by Java code, see for example this question.由于隐式广播接收器从 Android 8.0 开始不起作用,因此您必须通过代码以及清单中注册接收器。
执行以下步骤:
要使广播正常工作,您必须在服务中注册它。为了保持您的服务活跃,您可以使用与此问题无关的
警报管理器
、作业
等工具。Since implicit broadcast receivers are not working as of Android 8.0, you must register your receiver by code and also in the manifest.
Do these steps:
To make your broadcast work, you have to register it in service. And to keep your service alive you can use tools like
alarm managers
,jobs
, etc which is not related to this question.检查你的类名,它正在扩展BroadcastReceiver。它应该是“MyReciever”而不是“MyReciever”
Check your Class name, that is extending BroadcastReceiver. It should be "MyReciever" not "MyReiever"
除了前面的答案中提到的打字错误以及接收器类包名称应该在
receiver
标记中完全提及这一事实之外,我认为问题在于相关代码使用了两个嵌套的意图过滤器。我相信这段代码会正确工作:Beside Typo that mentioned in earlier answers and the fact that the receiver class package name should be completely mentioned in
receiver
tag, I think the problem is that the code in question uses two nested intent filters. I believe this code will work correctly:我只能给你一个快速提示,因为我已经走过了你所遵循的道路,但成功率要低得多。尝试使用 java.util.logging 读取 logcat,这样您就不需要读取日志的权限。并在日志视图中为包含“系统禁用”作为其标头的监听器创建监听器。它在锁定和解锁时都会启动。检查是否可以访问 system.android 而不是另一个屏幕。
希望有帮助。祝你好运
I can only give you a quick tip as i have gone through that path you are following with much less success. Try to read logcat using java.util.logging so that you will not require permission to read logs. And in log view create listener for the one containing "system disable" as its header. it fires up both at lock and unlock. check for the one that gives access to system.android not the other screen.
Hope it helps. Best of luck