I have code that sets a timer, but if the user sets a timer while using it, I need to remove the runnable that starts the timer, and start it again. But when no handler callback handler exists, and this code is called, it crashes my application. So I need to check if the handler works, if so, then finish it and restart, but, looking at the documentation and other Stackoverflow questions, I donβt see if this is possible.
Here is my code, I commented on the code, which should only be executed if there is a handler executable file:
submitTimer.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { String s = timer.getText().toString(); if(!s.equals("")) { //I NEED TO CHECK A RUNNABLE HANDLER EXISTS AND IF SO THEN RUN THIS CODE, IF NOT IGNORE THIS CODE Map.handler.removeCallbacks(Map.getRunnable()); Map.runnable.run(); //I NEED TO CHECK A RUNNABLE HANDLER EXISTS AND IF SO THEN RUN THIS CODE, IF NOT IGNORE THIS CODE int l = Integer.parseInt(s); Map.timerinmins = l; timer.setHint("Current Timer is "+Map.timerinmins); timer.setText(""); Toast.makeText(Preferences.this, "Timer is set!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(Preferences.this, "No value was entered", Toast.LENGTH_SHORT).show(); } } });
Can someone help me figure out a way to check the current state of the handlers?
deucalion0
source share