我创建了class
,它扩展了AsyncTask
,以与服务同步。同步完成后,我想将时间(hh:mm)放入TextView
中,以通知用户上次同步的时间。我在onPostExecute()
里面做。
问题是这种情况只会发生一次。 TextView稍后不会更新。
我很确定会调用doInBackground()
,因为就像应用程序启动时(需要很多记录)一样,同步需要花费很长时间,但是我不确定100%,因为数据收集不会根本没有改变。
那是我的AsyncTask
课:
private class Reconnect extends AsyncTask<String, Void, Void>{
private RotateAnimation anim = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f);
@Override
protected void onPreExecute() {
rlRefresh.setEnabled(false);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);
refreshIV.startAnimation(anim);
}
@Override
protected Void doInBackground(String... strings) {
retrofit2.Call<List<ServiceTaskAxapta>> call = api.getTasks("1", strings[0]);
retrofit2.Response<List<ServiceTaskAxapta>> response = null;
try {
response = call.execute();
} catch (IOException e) {
e.printStackTrace();
}
if(response.code() != 200){
Toast.makeText(context, "Error 1", Toast.LENGTH_SHORT).show();
} else {
for (int i = 0; i < response.body().size(); i++){
String endTime = DateParser.increaseTimeString(response.body().get(i).getReportTime(), 8, 0, false, null, null, null);
allTasksDB.insertIntoDB(response.body().get(i).getTaskID(), "path", response.body().get(i).getStreet(), response.body().get(i).getCity(), response.body().get(i).getPhoneNumber(),
response.body().get(i).getCompanyName(), Integer.parseInt(response.body().get(i).getReportDay()), Integer.parseInt(response.body().get(i).getReportMonth()),
Integer.parseInt(response.body().get(i).getReportYear()), Integer.parseInt(response.body().get(i).getDeadlineDay()), Integer.parseInt(response.body().get(i).getDeadlineMonth()),
Integer.parseInt(response.body().get(i).getDeadlineYear()), response.body().get(i).getReportTime(), endTime);
}
}
call = api.getTasks("2", strings[0]);
response = null;
try {
response = call.execute();
} catch (IOException e) {
e.printStackTrace();
}
if(response.code() != 200){
Toast.makeText(context, "Error 2", Toast.LENGTH_SHORT).show();
} else {
for (int i = 0; i < response.body().size(); i++){
String endTime = DateParser.increaseTimeString(response.body().get(i).getStartTimeMax(), 24, 0,false, null, null, null);
acceptedTasksDB.insertIntoDB(response.body().get(i).getTaskID(), "path", response.body().get(i).getStreet(), response.body().get(i).getCity(), response.body().get(i).getPhoneNumber(),
response.body().get(i).getCompanyName(), Integer.parseInt(response.body().get(i).getDeadlineDay()), Integer.parseInt(response.body().get(i).getDeadlineMonth()),
Integer.parseInt(response.body().get(i).getDeadlineYear()), Integer.parseInt(response.body().get(i).getAcceptedDay()), Integer.parseInt(response.body().get(i).getAcceptedMonth()),
Integer.parseInt(response.body().get(i).getAcceptedYear()), response.body().get(i).getStartTime(), endTime, response.body().get(i).getStartTimeMax(), false, false,
null, null);
}
}
call = api.getTasks("3", strings[0]);
response = null;
try {
response = call.execute();
} catch (IOException e) {
e.printStackTrace();
}
if(response.code() != 200){
Toast.makeText(context, "Error 3", Toast.LENGTH_SHORT).show();
} else {
for (int i = 0; i < response.body().size(); i++){
acceptedTasksDB.insertIntoDB(response.body().get(i).getTaskID(), "path", response.body().get(i).getStreet(), response.body().get(i).getCity(), response.body().get(i).getPhoneNumber(),
response.body().get(i).getCompanyName(), Integer.parseInt(response.body().get(i).getDeadlineDay()), Integer.parseInt(response.body().get(i).getDeadlineMonth()),
Integer.parseInt(response.body().get(i).getDeadlineYear()), Integer.parseInt(response.body().get(i).getAcceptedDay()), Integer.parseInt(response.body().get(i).getAcceptedMonth()),
Integer.parseInt(response.body().get(i).getAcceptedYear()), response.body().get(i).getStartTime(), null, response.body().get(i).getStartTimeMax(), true, false,
null, null);
}
}
call = api.getTasks("4", strings[0]);
response = null;
try {
response = call.execute();
} catch (IOException e) {
e.printStackTrace();
}
if(response.code() != 200){
Toast.makeText(context, "Error 4", Toast.LENGTH_SHORT).show();
} else {
for (int i = 0; i < response.body().size(); i++){
acceptedTasksDB.insertIntoDB(response.body().get(i).getTaskID(), "path", response.body().get(i).getStreet(), response.body().get(i).getCity(), response.body().get(i).getPhoneNumber(),
response.body().get(i).getCompanyName(), Integer.parseInt(response.body().get(i).getDeadlineDay()), Integer.parseInt(response.body().get(i).getDeadlineMonth()),
Integer.parseInt(response.body().get(i).getDeadlineYear()), Integer.parseInt(response.body().get(i).getAcceptedDay()), Integer.parseInt(response.body().get(i).getAcceptedMonth()),
Integer.parseInt(response.body().get(i).getAcceptedYear()), response.body().get(i).getStartTime(), null, response.body().get(i).getStartTimeMax(), true, true,
null, null);
}
}
call = api.getTasks("5", strings[0]);
response = null;
try {
response = call.execute();
} catch (IOException e) {
e.printStackTrace();
}
if(response.code() != 200){
Toast.makeText(context, "Error 5", Toast.LENGTH_SHORT).show();
} else {
dbConnectionProblems.open();
for (int i = 0; i < response.body().size(); i++){
acceptedTasksDB.insertIntoDB(response.body().get(i).getTaskID(), "path", response.body().get(i).getStreet(), response.body().get(i).getCity(), response.body().get(i).getPhoneNumber(),
response.body().get(i).getCompanyName(), Integer.parseInt(response.body().get(i).getDeadlineDay()), Integer.parseInt(response.body().get(i).getDeadlineMonth()),
Integer.parseInt(response.body().get(i).getDeadlineYear()), Integer.parseInt(response.body().get(i).getAcceptedDay()), Integer.parseInt(response.body().get(i).getAcceptedMonth()),
Integer.parseInt(response.body().get(i).getAcceptedYear()), response.body().get(i).getStartTime(), null, response.body().get(i).getStartTimeMax(), true, false,
null, null);
dbConnectionProblems.insertIntoDB(response.body().get(i).getTaskID(), true, null, null, null, null);
}
dbConnectionProblems.close();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
if(!anim.hasEnded()){
refreshIV.setAnimation(null);
}
String lastSynchro = calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE) + "@" + calendar.get(Calendar.DAY_OF_MONTH) + "-" + (calendar.get(Calendar.MONTH)-1) + "-" + calendar.get(Calendar.YEAR);
ProjectStuff.saveLastSynchro(lastSynchro, context);
String synchroTime = "Last synchro: " + DateParser.parseTime(context, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE));
tvSynchroInfo.setText(synchroTime);
rlRefresh.setEnabled(true);
}
}
还有execute()
方法:
rlRefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Reconnect refreshEvent = new Reconnect();
refreshEvent.execute("12");
}
});
经过一段时间(需要同步)后,胺化作用停止,因此看起来好像调用了onPostExecute()并更新了TextView,但是以后的每次调用都不会更新它。
答案 0 :(得分:0)
好的,我发现了问题。我在calendar = Calendar.getInstance()
方法中调用了onCreate()
,
我应该在保存之前先在onPostExecute()
中进行此操作,然后在TextView
中显示它以获取正确的时间。 br />