2012-02-11 66 views
1

我用这样的设置为我的节目:防止接收广播多次

IntentFilter filter = new IntentFilter("com.commonsware.cwac.tlv.demo.onlineDbResult"); 
    filter.addCategory(INTENT_CATEGORY); 
    ResultReceiver receiver= new ResultReceiver(); 
    registerReceiver(receiver, filter); 

而且调用接收器:

Intent resultIntent = new Intent("com.commonsware.cwac.tlv.demo.onlineDbResult"); 
     if(categories!=null){ 
      for(String category:categories){ 
       resultIntent.addCategory(category); 
       Log.d(TAG,"add category "+category); 
      } 
     } 

不知怎的,在这种方式中注册的接收器接收的意图多次(2或3次)这是为什么?

com.commonsware.cwac.tlv.demo是命名空间,onlineDbResult只是一个附加字符串,不是类或任何东西。

的的onReceive:

@Override 
     public void onReceive(Context context, Intent intent) { 
      Bundle extras = intent.getExtras(); 
      String result = extras.getString("result"); 
      Log.d("baby","Register received result "+result); 
      if(progressDialog!=null) 
       progressDialog.dismiss(); 
      if(result.equals("user_added")){ 
       do stuff 

回答

0

回想起来,我认为我的问题可能是我混合了类别和操作。过滤器与分类和动作相匹配,并分别为两个原因捕获了意图,两次触发接收。但我不确定。为了防止这种情况,请做出明智的决定,以便使用类别或操作

0

不知何故以这种方式登记的接收器接收的意图多次(2个或三次)这是为什么?

要么你注册了多个接收器,要么你广播多个Intents

请注意,我不知道为什么你在用广播混淆类别。类别主要用于活动。

+0

建议的方法是在发送时只使用动作,而在接收端使用过滤器接受多个动作(如果需要)? 我添加了收件人。建议的做法是测试行动而不是我做出的额外“结果”? – 2012-02-11 14:40:22

1

检查您是否正确注销接收方,即onDestroy(),否则您可能会收到两次相同的Intent。