Stop the handler inside itself

MrPencil

I am trying to stop the Handler inside the handler self but I am getting this error. How can I stop the Handler in this case?

Cannot instantiate the type Runnable

Code:

    new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    senseWiFi();
                    if(WIFINumberList.size() > 1){
                        int first = WIFINumberList.get(0);
                        int second = WIFINumberList.get(1);
                        if(first == second){
                            route_number = first;
                            System.out.println("route equal route_number.");
                       //Here I mam getting the error.
                            new Handler().removeCallbacks(new Runnable());
                        }else{
                            System.out.println("route equal ZERO.");
                        }

                    }
                }
            }, 1*30 * 1000);
ρяσѕρєя K

Here:

new Handler().removeCallbacks(new Runnable());

Means creating new object of Handler and for removing callback from Handler passing new object of Runnable.

Instead of this create a separate object of Handler and Runnable like:

Handler handler=new Handler();
handler.postDelayed(runnable);
Runnable runnable =new Runnable() {
      @Override
       public void run() {
        // your code here
        //remove callback here
      handler.removeCallbacks(runnable);
     }
}

means use same object of both Handler and Runnable which is used for calling postDelayed method instead of creating new Object

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related