我正试图通过Service
中的广播传播arraylist in this like
ArrayList objects = new ArrayList();
for (MasterVO vo : masterVOs) {
objects.add(vo.getMasterTables());
objects.add(vo.getDeviceUpdateStatus());
}
Bundle extra = new Bundle();
extra.putSerializable("objects", objects);
intent.putExtra("extra", extra);
sendBroadcast(intent);
然后进入活动:
private void updateUI(Intent intent) {
Bundle extra = getIntent().getBundleExtra("extra");
ArrayList<Object> databaseList = (ArrayList<Object>) extra.getSerializable("objects");
fillMasterTable(databaseList);
}
广播的onReceive()
:
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateUI(intent);
}
};
我在null
收到了extra
。我究竟做错了什么?我看到`sendBroadcast'部分有值。
答案 0 :(得分:2)
您正试图getIntent
而不是 activity
broadcast receiver
。使用intent
中收到的onReceive
。
public void onReceive(Context context, Intent intent) {
updateUI(intent);
}
private void updateUI(Intent intent) {
Bundle extra = intent.getBundleExtra("extra"); //use intent NOT getIntent()
ArrayList<Object> databaseList = (ArrayList<Object>)
extra.getSerializable("objects");
fillMasterTable(databaseList);
}