我正试图通过aService
的广播传递arraylist,意图如下
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”部分有值。 最佳答案
您试图从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);
}