Run a method every x seconds and stop the method after certain conditions

1.5k views Asked by At

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

1

There are 1 answers

0
Rodrigo Fernandes On

Have you considered using a Handler and a Runnable? Like so:

private int handler_time = 5000 //5 seconds in milliseconds
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {

    @Override
    public void run() {
        //Your function
        getBookingInfo();
        if(YourCondition){
            //showBookedDialog()
        }else{
            //restarting the runnable
            handler.postDelayed(this, handler_time);
        }
    }

};

To start the runnable, just call

handler.post(runnable);

And to end it

handler.removeCallbacks(runnable);

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

if(!dialogIsShowing){
    //showBookedDialog()
}

When you close your dialog, you put that variable as false