0

I want to stop the handler in onDestroy(). The code as follows. blink() method call on for particular reason in activity but want to stop their service in to the destroy method.

final Handler handler = new Handler();
private void blink() {
    PrintLog.log("On", "Blink Thread");
    new Thread(new Runnable() {
        @Override
        public void run() {
            int timeToBlink = 1000;    //in milissegunds
            try {
                Thread.sleep(timeToBlink);
            } catch (Exception e) {
            }
            handler.post(new Runnable() {
                @Override
                public void run() {

                    if (text_ATMCardInstruction.getVisibility() == View.VISIBLE) {
                        text_ATMCardInstruction.setVisibility(View.INVISIBLE);
                    } else {
                        text_ATMCardInstruction.setVisibility(View.VISIBLE);
                    }
                    blink();
                }
            });
        }
    }).start();
}

@Override
protected void onDestroy() {

    // what is code here?
    PrintLog.log("Stop", "serviceStop");
    super.onDestroy();
}
Rohit B.
  • 5
  • 5

1 Answers1

0

Handle on treadRunning boolean value onDestroy() method set treadRunning = false;

private void blink() {
    PrintLog.log("On", "Blink Thread");

    if (treadRunning) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                int timeToBlink = 1000;    //in milissegunds
                try {
                    Thread.sleep(timeToBlink);
                } catch (Exception e) {
                }
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (text_ATMCardInstruction.getVisibility() == View.VISIBLE) {
                            text_ATMCardInstruction.setVisibility(View.INVISIBLE);
                        } else {
                            text_ATMCardInstruction.setVisibility(View.VISIBLE);
                        }
                        blink();
                    }
                });
            }
        }).start();
    } else {
        PrintLog.log("On", "Blink Thread Stop");
        new Thread(new Runnable() {
            @Override
            public void run() {
                text_ATMCardInstruction.setVisibility(View.INVISIBLE);
            }
        }).interrupt();
    }
}
Rohit B.
  • 5
  • 5