My user clicks a button. 500 milliseconds after the button get's pressed I want to start the next action. After the button is pressed my app usually does 50-100 milliseconds of additional processsing.
Therefore I start a a new thread right after the button get's pressed via my class:
private class WaitForUpdating extends Thread {
/** The app waits for duration of the passed time (in ms)
and afterwards loads the next card.
*/
public WaitForUpdating(int time) {
this.start();
}
public void run() {
try {
Thread.sleep(500);
Message msg = new Message();
Bundle b = new Bundle();
b.putInt("State", UPDATING);
msg.setData(b);
mHandler.sendMessage(msg);
} catch (InterruptedException e) {
}
}
}
Then I try to handle the result via:
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Bundle b = msg.getData();
int state = b.getInt("State");
if (state == ScoreAndTimeFragment.UPDATING) {
try {
setCard();
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
The Android linter shows me that this produces a Handler leak. What can I do to not have the leak in this case?