无法将数据从Runnable类传递到Service类

时间:2018-05-02 09:27:26

标签: java android

我想将数据传递给我的服务类。我的模型使用implements Serializable创建。

Runnable class

Bundle bundle = new Bundle();
Intent serviceIntent = new Intent(mContext,InitLocationsOnStartup.class);
List<StationViewModel> stationList = new ArrayList<StationViewModel>();
......
....
bundle.putSerializable("stations",stationList); //Wrong 2nd argument type. Found: 'java.util.List<StationViewModel>', required: 'java.io.Serializable'

  serviceIntent.putExtra("stations",stationList);//Cannot resolve method 'putExtra(java.lang.String, java.util.List<StationViewModel>

如何传递数据?

2 个答案:

答案 0 :(得分:1)

您需要 stationList List bundle.putSerializable("stations", (Serializable) stationList);

implements
  

StationViewModel 可序列化到您的 public class StationViewModel implements Serializable

<强> var divs = document.getElementsByClassName('block-text'); var allDivsHeight = []; for (i = 0; i < divs.length; i++) { allDivsHeight.push(divs[i].offsetHeight); } console.log(allDivsHeight);//just for debugging console.log(Math.max(...allDivsHeight))

答案 1 :(得分:1)

尝试序列化您的数据

bundle.putSerializable("stations", (Serializable) stationList);

Intent serviceIntent = new Intent(YourActivity.this, bgservice.class);
serviceIntent.putExtra("stations", (Serializable) stationList);
this.startService(serviceIntent);