在广告中的intent.putExtra中传递arraylist在getBundleExtra

时间:2016-04-05 06:57:07

标签: java android android-intent arraylist

我正试图通过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'部分有值。

1 个答案:

答案 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);
}