2014-10-07 71 views
0

我有一個與LocalBroadcastManager一樣的動作欄活動,其定義與回答here中的完全相同,唯一的區別在於它是在ActionBarActivity中定義的。Android在ActionBarActivity中接收廣播

出於某種原因,無論我嘗試什麼,我都無法找到接收者的onReceive(即成功接收廣播消息)。

服務代碼:

public class GcmIntentService extends IntentService { 

@Override 
protected void onHandleIntent(Intent intent) { 
    Intent toDrawerActivity = new Intent(syncActionName); 
    String syncType = extras.getString("data"); 
    toDrawerActivity.putExtra("syncType", syncType); 
    System.out.println("sending intent in service"); 
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent); 
} 

}

而且活動代碼:

public class DrawerActivity extends ActionBarActivity { 
private BroadcastReceiver dataUpdaterReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
    System.out.println("in broadcast receiver"); 
    } 
}; 

protected void onCreate(){ 
... 
... 
LocalBroadcastManager.getInstance(this).registerReceiver(dataUpdaterReceiver, 
     new IntentFilter(GcmIntentService.syncActionName)); 
} 

protected void onDestroy(){ 
    LocalBroadcastManager.getInstance(this).unregisterReceiver(dataUpdaterReceiver); 

} 

} 

我究竟錯在這裏做什麼?

回答

2

你有一個問題,由於你使用sendBroadcast錯誤的參數()方法:

LocalBroadcastManager.getInstance(this).sendBroadcast(intent); 

它應該是:

LocalBroadcastManager.getInstance(this).sendBroadcast(toDrawerActivity);