Previously this code worked perfect.
Now its is showing android.os.handler is deprecated.
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_STATE_CHANGE:
break;
}
}
How can we resolve this issue.
removecallback and handler = null; to cancel out the handle just to keep the code clean and make sure everything will be removed.
The constructor new Handler(Looper. myLooper(), callback) is the exact alternative to the aforementioned deprecated methods. All these use the local thread to execute the background task.
Just use the removeCallbacks(Runnable r) method.
postDelayed(Runnable r, Object token, long delayMillis) Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses. final void. removeCallbacks(Runnable r) Remove any pending posts of Runnable r that are in the message queue.
As mentioned by Mike in the comments, Handler
is not deprecated. The way of creating an object of Handler using new Handler()
is deprecated.
As per the documentation, using new Handler()
can lead to bugs. So you should specify a looper for the handler explicitly. Looper must not be null.
Refer the code:
private final Handler mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_STATE_CHANGE:
break;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With