Android - 意图以某种方式增加?
我已经开始在我的应用程序中使用自定义意图,但遇到了一些问题。
当我发送自定义意图时,我注册了一个广播接收器,并且我捕获意图没有问题。
然而,当我再次发送意图时,似乎出现了问题,广播接收器似乎注册了两个意图事件,依此类推,因此,如果第三次发送意图,我会收到 3 次。
这在我的应用程序中造成了重大问题,我想知道这是否正常,我必须采取某种方法来处理它吗?
这是我的代码:
发送意图:
Intent i = new Intent();
i.setAction(SIP_INCOMING_CALL_CANCEL_INTENT);
sendBroadcast(i);
接收意图:
sipIncomingListener = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(CallDialogActivity.SIP_INCOMING_CALL_ANSWER_INTENT.equals(action)){
Log.d("SIPENGINE", "CALL CONNECTED SENT FROM INITINCOMINGLISTENER()");
}
};
IntentFilter filter = new IntentFilter(CallDialogActivity.SIP_INCOMING_CALL_CANCEL_INTENT);
registerReceiver(sipIncomingListener, filter);
是否有办法确保意图仅被触发一次?
I have started using custom intents in my application and I have come across a bit of a problem.
When I send a custom intent I register a Broadcast Receiver and I catch the intent no problem.
However problems seem to appear when I send the intent again, the Broadcast Reciever seems to register two events of the intent and so on so if the intent is sent a third time I recieve it 3 times.
This is causing major problems in my application and was wondering is it normal and there is some way I have to deal with it?
Here is my code:
To Send the Intent:
Intent i = new Intent();
i.setAction(SIP_INCOMING_CALL_CANCEL_INTENT);
sendBroadcast(i);
To receive the Intent:
sipIncomingListener = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(CallDialogActivity.SIP_INCOMING_CALL_ANSWER_INTENT.equals(action)){
Log.d("SIPENGINE", "CALL CONNECTED SENT FROM INITINCOMINGLISTENER()");
}
};
IntentFilter filter = new IntentFilter(CallDialogActivity.SIP_INCOMING_CALL_CANCEL_INTENT);
registerReceiver(sipIncomingListener, filter);
Is there anyway to make sure the Intent is only fired once??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定为什么你会遇到这个问题,但是你确定你需要一个广播(接收器)来处理意图吗?
您能解释一下,为什么要这样做吗?
回到你的问题:你能提供一个简单的简约项目吗?我认为问题不在于您提供的代码中。
I'm not sure, why you have this issue, but are you sure that you need a Broadcast(Receiver) to handle intents?
Can you please explain, why do you do that?
Back to your problem: can you provide a simple minimalistic project? I think the issue is not in the code you provided.