i currently checking every 5 seconds that status of the response that i'm getting from the back-end.
here is my code:
void getBookingInfo() {
WebApi client = ServiceGenerator.createService(WebApi.class);
final Call<BaseResponse <List<MemberBookingInfo>>> call = client.getBookingInfo(queries);
call.enqueue(new Callback<BaseResponse<List<MemberBookingInfo>>>() {
@Override
public void onResponse(Call<BaseResponse<List<MemberBookingInfo>>> call, Response<BaseResponse<List<MemberBookingInfo>>> response) {
if (response.isSuccessful()) {
if (checkIfAccepted(response.body().data.get(response.body().data.size()-1).status)){
showBookedDialog(response.body().data.get(response.body().data.size()-1));
}
@Override
public void onFailure(Call<BaseResponse<List<MemberBookingInfo>>> call, Throwable t) {
Log.d("ERROR", "" + t.getMessage());
}
});
}
}
here is my timer where runs again booking info for 5 seconds
boolean checkIfAccepted(int status){
if (status == 1){
return true;
}
hourglass = new Hourglass(5000, 1000) {
@Override
public void onTimerTick(long timeRemaining) { // Update UI
}
@Override
public void onTimerFinish() { // Timer finished
getBookingInfo();
}
};
if (!hourglass.isRunning()) {
hourglass.startTimer();
}
return false;
}
if my conditions were met the hourglass should stop and this dialogs should show:
public void showBookedDialog(final MemberBookingInfo memberBookingInfo){
listener.stopBooking();
dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog_booking);
if(!((Activity) context).isFinishing()) {
if (dialogShowing) {
dialog.show();
dialogShowing = true;
}
}
The problem is : 1. my activity shows around 5 - 10 stacks of dialogs. 2. i'm looking for a better method to call a method for every 5 seconds without consuming much memory
Have you considered using a Handler and a Runnable? Like so:
To start the runnable, just call
And to end it
You can also add a global variable that stores a boolean about the state of the dialog (ie. If the dialog is showing or not) and create a condition where
When you close your dialog, you put that variable as
false