我创建了一个IntentService,如下所示:
`
protected void onHandleIntent(@Nullable Intent intent) {
Realm.init(this);
realm = Realm.getDefaultInstance();
List<DbModel> list = new ArrayList<>();
RealmResults<DbModel> users =
realm.where(DbModel.class).equalTo("Status", false)
.findAll();
list.addAll(users);
for (int i = 0; i < list.size(); i++)
sendMeterReading(list.get(i));
}
private void sendMeterReading(DbModel dbModel) {
propertyid = dbModel.getPropertyId();
MeterReadingModel meterReadingModel = new MeterReadingModel();
MeterReadingBuilder mobileDeviceRegBuilder = new MeterReadingBuilder()
.setAmrModuleSerial(dbModel.getAmrModuleSerial())
.setPropertyId(dbModel.getPropertyId()
meterReadingModel.getMeterDeviceModel().add(mobileDeviceRegBuilder
.createMobileDeviceReg());
RestClient.getInstance(this).get().sendReading(meterReadingModel).enqueue(this);
}
public void onResponse(@NonNull Call<CommonResponse> call, @NonNull Response<CommonResponse> response) {
if (response.body().getStatusCode() == Constants.SUCCESS_CODE) {
Realm.init(this);
realm = Realm.getDefaultInstance();
DbModel dbModel = realm.where(DbModel.class).equalTo("propertyId",
propertyid).findFirst();
realm.beginTransaction();
dbModel.setUpdateStatus(true);
realm.insertOrUpdate(dbModel);
realm.commitTransaction();
}
}
`
在上面的代码中,我使用的是realm数据库,并且有多个记录,我只需要少量记录并使用改进命中API,我使用意向服务,当我得到一个成功的结果然后我更新我的DB.Here我的问题是当我调用intent服务时。 它只打了一个记录,但我传了多个记录。
我从Activity的onResume方法调用此服务。因此,每当活动恢复时,都会调用此服务:
@Override
protected void onResume() {
super.onResume();
Intent updateService = new Intent(HomeActivity.this, UpdateService.class);
startService(UpdateService);
}