从广播接收器完成活动
我有一个活动,当电话响起时(通过电话应用程序)显示为无模式。我想在发生以下任一事件时完成活动。第一个是我触摸活动之外的任何地方(这不是问题),第二个是铃声是否停止。我正在广播接收器中监听 IDLE_STATE,但我不确定当我看到它时如何调用活动的结束。接收器不是由 Activity 注册的,而是由 Manifest.xml 注册的
I have an Activity that I display as modeless when the phone rings (over the phone app). I would like to finish the Activity when either of the following events occur. The first is if I touch anywhere outside the Activity (this is not a problem), the second is if the ringing stops. I am listening for IDLE_STATE in the broadcast receiver but I am not sure on how to call the finish on the activity when I see it. The receiver is not registered by the activity but by the Manifest.xml
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我使用这段代码来解决我的问题。
finish
是Activity
的方法,因此调用Activity
和上下文如下:I used this code to solve my problem.
finish
is the method ofActivity
, so callActivity
and context like:现在在您的接收广播中编写代码,这将发送另一个广播,其意图名为“com.hello.action”
现在在活动中捕获此意图,您想要完成它,然后在 onReceive 上调用 super.finish()您的接收器的方法
像这样
这将完成活动
write the code in your receiving broadcast now this will send another broad cast with the intent named "com.hello.action"
Now catch this intent in the activity with you want to finish it and then call the super.finish() on the onReceive method of your receiver
like this
this will finish the activity
如果您从活动中注册另一个广播接收器怎么办?然后,当您想杀死它时,从您提到的广播接收器发送广播消息。
What if you register another broadcast receiver from the activity. Then, when you want to kill it, send a broadcast message from the broadcast receiver that you mentioned.
实际上,我最终在 Activity 中添加了一个 PhoneStateListener 来监听 IDLE_STATE。
I actually ended up adding a PhoneStateListener in the Activity to listen for IDLE_STATE.
经过长时间的互联网研发,我解决了我的问题。如果您从广播接收器启动一个活动并且
清除所有活动堆栈实例。
After a long R&D over internet i have solved my problem. The below code is helpful if you start an activity from broadcast receiver and
clear all activity stack instance.