我正试图通过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而不是activitybroadcast 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);
}

07-25 22:22