Issue
I've created class
which extends AsyncTask
, to synchronize with service. After synchro is finished I want to put the time (hh:mm) into TextView
, to inform user, when synchro was made last time. I am doing it inside onPostExecute()
.
Problem is that this happens only once. TextView doesn't get updated later.
I'm prettey sure doInBackground()
is being called because it takes quite a long time to synchronize just like when application starts (there is a lot of records), but I can't be 100% sure since data collection doesn't change at all.
That's my AsyncTask
class:
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);
}
}
And the execute()
method:
rlRefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Reconnect refreshEvent = new Reconnect();
refreshEvent.execute("12");
}
});
After amount of time (needed to synchronize), amination stops, so it looks like onPostExecute() is called, and TextView is updated, but every later call doesn't update it.
Solution
Ok, I've found the problem. I called calendar = Calendar.getInstance()
inside onCreate()
method,
I should have done this in onPostExecute()
right before saving it, and showing it inside TextView
to get right time.
Answered By - SkypeDogg
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.