12

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.

GNK
  • 1,036
  • 2
  • 10
  • 29
  • 6
    `Handler` is not deprecated. Look more closely at the error message. That particular constructor is deprecated: https://developer.android.com/reference/android/os/Handler#Handler(). – Mike M. Aug 21 '20 at 05:56
  • check this answer it worked https://stackoverflow.com/a/63851895/5773037 – Nikunj Paradva Oct 28 '20 at 04:57

1 Answers1

29

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;
        }
    }
Alpha 1
  • 4,118
  • 2
  • 17
  • 23