2017-02-03 51 views
0

我想廣播只爲特定的活動。如果活動破壞,則廣播不會觸發。 我使用這樣的如何使廣播只爲特定的活動生活在android

protected void onStart() { 
     super.onStart(); 
     LocalBroadcastManager.getInstance(this).registerReceiver((receiver), 
       new IntentFilter(GCMIntentService.COPA_RESULT) 
     ); 
    } 
receiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       int count = intent.getIntExtra(GCMIntentService.COPA_MESSAGE, 0); 
       int youCount = intent.getIntExtra(GCMIntentService.COUNT_YOU, 0); 
       int reqCount = intent.getIntExtra(GCMIntentService.COUNT_REQ, 0); 
       if (count != 0) { 
        notificationcount.setVisibility(View.VISIBLE); 
        notificationcount.setText("" + count); 
       } else { 
        notificationcount.setVisibility(View.GONE); 
       } 
       AppCommon.notification_requestcount = reqCount; 
       AppCommon.notification_youcount = youCount; 
       AppCommon.notification_total_count = count; 
      } 
     }; 

什麼我失蹤如果你想這樣做,則請提出這個

+1

您嘗試過什麼嗎?如果是這樣,那麼編輯這個問題,以節省投票! – OBX

+1

您是否嘗試在您的活動中接收或發送廣播? –

+0

考慮接受一個答案,如果幫助..! – W4R10CK

回答

0

,只需做一件事註冊廣播接收器在活動本身和在同一活動註銷。如下所示

@Override 
    protected void onStart() { 
     super.onStart(); 
     LocalBroadcastManager.getInstance(this).registerReceiver((receiver), 
       new IntentFilter(GCMIntentService.COPA_RESULT) 
     ); 
    } 

    @Override 
    protected void onStop() { 
     LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver); 
     super.onStop(); 
    } 

receiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       int count = intent.getIntExtra(GCMIntentService.COPA_MESSAGE, 0); 
       int youCount = intent.getIntExtra(GCMIntentService.COUNT_YOU, 0); 
       int reqCount = intent.getIntExtra(GCMIntentService.COUNT_REQ, 0); 
       if (count != 0) { 
        notificationcount.setVisibility(View.VISIBLE); 
        notificationcount.setText("" + count); 
       } else { 
        notificationcount.setVisibility(View.GONE); 
       } 
       AppCommon.notification_requestcount = reqCount; 
       AppCommon.notification_youcount = youCount; 
       AppCommon.notification_total_count = count; 
      } 
     }; 
+0

讓我試試看 –

+0

快點!它的工作 –

0

Activity內部創建接收器並添加屬性。同時,如果您希望Activity分別銷燬registerunregister時分別銷燬onResume()onPause(),銷燬接收器。

演示:

private BroadcastReceiver receiver = new BroadcastReceiver() { 
@Override 
public void onReceive(Context context, Intent intent) { 
     // your methods and actions for receiver 
    } 
}; 

註冊廣播在活動的onCreate()方法:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.your_layout); 
    your_activity_.this.registerReceiver(receiver, new IntentFilter(your_filter_action)); 
    .. 
    ... 
    .... 
    } 

不要忘記註冊的/在活動週期註冊

你的活動創建BroadcastReceiver

@Override 
protected void onResume() { 
    super.onResume(); 
    your_activity.this.registerReceiver(receiver, new IntentFilter(your_filter_action)); 

} 

@Override 
protected void onPause() { 
    super.onPause(); 
    your_activity.this.unregisterReceiver(receiver); 

} 
+1

好的。我會嘗試這種方式來執行功能 –

+0

@Abhinavkorpal,考慮接受答案,如果幫助! – W4R10CK

+0

@ WAR10CK肯定! –

0

您忘記取消註冊您的廣播接收機,請檢查。

@Override 
    protected void onStop() { 
     LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver); 
     super.onStop(); 
    } 
+0

是啊!正確的感謝和工作 –

相關問題