Android - 意图以某种方式增加?

发布于 2024-08-23 12:00:46 字数 905 浏览 9 评论 0原文

我已经开始在我的应用程序中使用自定义意图,但遇到了一些问题。

当我发送自定义意图时,我注册了一个广播接收器,并且我捕获意图没有问题。

然而,当我再次发送意图时,似乎出现了问题,广播接收器似乎注册了两个意图事件,依此类推,因此,如果第三次发送意图,我会收到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生寂 2024-08-30 12:00:46

我不确定为什么你会遇到这个问题,但是你确定你需要一个广播(接收器)来处理意图吗?
您能解释一下,为什么要这样做吗?

回到你的问题:你能提供一个简单的简约项目吗?我认为问题不在于您提供的代码中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文