我创建了扩展AsyncTask
的class
,以与服务同步。同步完成后,我想将时间(hh:mm)放入TextView
,以通知用户上次进行同步的时间。我在onPostExecute()
里面做.
问题是这种情况只发生一次。文本视图以后不会更新。
我确信正在调用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 被更新,但以后的每次调用都不会更新它。
好的,我发现了问题。我在方法中调用calendar = Calendar.getInstance()
onCreate()
,
我应该在保存它之前立即在onPostExecute()
中执行此操作,并在TextView
内部显示它以获得正确的时间。