提问者:小点点

处理程序(Handler. Callback)已弃用


处理程序(android. os.Handler.Callback)已弃用,我应该使用什么来代替?

Handler handler = new Handler(new Handler.Callback() {
    @Override
    public boolean handleMessage(@NonNull Message message) {
        switch(message.what) {
            case READ_MESSAGE:
                byte[] readBuff = (byte[]) message.obj;
                String tempMessage = new String(readBuff, 0, message.arg1);
                readMsg.setText(tempMessage);
                break;
        }
        return true;
    }
});

共3个答案

匿名用户

从API级别30开始,有2个构造函数被弃用。

>

  • 处理程序()

    处理程序(处理程序.回调)

    谷歌在下面解释了原因。

    在Handler构建过程中隐式选择Looper可能会导致错误,其中操作会静默丢失(如果Handler不期望新的任务和退出)、崩溃(如果有时在没有Looper活动的线程上创建处理程序)或竞争条件,其中处理程序关联的线程不是作者预期的。相反,使用执行器或显式指定Looper,使用Looper#getMainLooper、{link android. view.View#getHandler}或类似方法。如果兼容性需要隐式线程本地行为,请使用new Handler(Looper.myLooper()、callback)向读者明确说明。

    解决方案1:使用执行器

    1、在主线程执行代码。

    // Create an executor that executes tasks in the main thread. 
    Executor mainExecutor = ContextCompat.getMainExecutor(this);
    
    // Execute a task in the main thread
    mainExecutor.execute(new Runnable() {
        @Override
        public void run() {
            // You code logic goes here.
        }
    });
    

    2.在后台线程中执行代码

    // Create an executor that executes tasks in a background thread.
    ScheduledExecutorService backgroundExecutor = Executors.newSingleThreadScheduledExecutor();
    
    // Execute a task in the background thread.
    backgroundExecutor.execute(new Runnable() {
        @Override
        public void run() {
            // Your code logic goes here.
        }
    });
    
    // Execute a task in the background thread after 1 second.
    backgroundExecutor.schedule(new Runnable() {
        @Override
        public void run() {
            // Your code logic goes here
        }
    }, 1, TimeUnit.SECONDS);
    

    注意:使用后记得关闭执行器。

    backgroundExecutor.shutdown(); // or backgroundExecutor.shutdownNow();
    

    3.在后台线程执行代码,在主线程更新UI。

    // Create an executor that executes tasks in the main thread. 
    Executor mainExecutor = ContextCompat.getMainExecutor(this);
    
    // Create an executor that executes tasks in a background thread.
    ScheduledExecutorService backgroundExecutor = Executors.newSingleThreadScheduledExecutor();
    
    // Execute a task in the background thread.
    backgroundExecutor.execute(new Runnable() {
        @Override
        public void run() {
            // Your code logic goes here.
            
            // Update UI on the main thread
            mainExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    // You code logic goes here.
                }
            });
        }
    });
    

    解决方案2:使用以下构造函数之一显式指定Looper。

    >

  • 处理程序(循环)

    处理程序(循环,处理程序。回调)

    1、在主线程执行代码

    1.1。带有Looper的处理程序

    Handler mainHandler = new Handler(Looper.getMainLooper());
    

    1.2带有Looper和Handler. Callback的处理程序

    Handler mainHandler = new Handler(Looper.getMainLooper(), new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            // Your code logic goes here.
            return true;
        }
    });
    

    2.在后台线程中执行代码

    2.1。带有Looper的处理程序

    // Create a background thread that has a Looper
    HandlerThread handlerThread = new HandlerThread("HandlerThread");
    handlerThread.start();
    
    // Create a handler to execute tasks in the background thread.
    Handler backgroundHandler = new Handler(handlerThread.getLooper()); 
    

    2.2.带有Looper和Handler. Callback的处理程序

    // Create a background thread that has a Looper
    HandlerThread handlerThread = new HandlerThread("HandlerThread");
    handlerThread.start();
    
    // Create a handler to execute taks in the background thread.
    Handler backgroundHandler = new Handler(handlerThread.getLooper(), new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            // Your code logic goes here.
            return true;
        }
    });
    

    注意:使用后记得松开线程。

    handlerThread.quit(); // or handlerThread.quitSafely();
    

    3.在后台线程执行代码,在主线程更新UI。

    // Create a handler to execute code in the main thread
    Handler mainHandler = new Handler(Looper.getMainLooper());
    
    // Create a background thread that has a Looper
    HandlerThread handlerThread = new HandlerThread("HandlerThread");
    handlerThread.start();
    
    // Create a handler to execute in the background thread
    Handler backgroundHandler = new Handler(handlerThread.getLooper(), new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            // Your code logic goes here.
            
            // Update UI on the main thread.
            mainHandler.post(new Runnable() {
                @Override
                public void run() {
                    
                }
            });
            
            return true;
        }
    });
    

  • 匿名用户

    在Java文件中使用它,

    Handler handler = new Handler(Looper.getMainLooper());
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
        //Code here
       }
    }, 2000);

    匿名用户

    在静态编程语言中,如果您使用HandlerRunnable,只需添加一个键:

    之前

     private var handler: Handler = Handler()
    

    之后

    private var handler: Handler = Handler(Looper.getMainLooper())