handler(handler.callback)已弃用

vulvrdjw  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(591)

handler(android.os.handler.callback)已弃用,我应该使用什么替代?

  1. Handler handler = new Handler(new Handler.Callback() {
  2. @Override
  3. public boolean handleMessage(@NonNull Message message) {
  4. switch(message.what) {
  5. case READ_MESSAGE:
  6. byte[] readBuff = (byte[]) message.obj;
  7. String tempMessage = new String(readBuff, 0, message.arg1);
  8. readMsg.setText(tempMessage);
  9. break;
  10. }
  11. return true;
  12. }
  13. });
iecba09b

iecba09b1#

在api级别30中,有2个构造函数已被弃用。
处理程序()
处理程序(handler.callback)
谷歌解释了以下原因。
在处理程序构造期间隐式地选择一个循环器可能会导致错误,其中操作会自动丢失(如果处理程序不需要新任务并退出)、崩溃(如果处理程序有时是在没有活动循环器的线程上创建的)或争用条件,其中处理程序关联的线程不是作者预期的。相反,使用执行器或显式指定循环器,使用looper#getmainlooper、{link android.view.view#gethandler}或类似的方法。如果兼容性需要隐式线程本地行为,请使用新处理程序(looper.mylooper(),callback)向读者说明。
解决方案1:使用执行器
1.在主线程中执行代码。

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

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

  1. // Create an executor that executes tasks in a background thread.
  2. ScheduledExecutorService backgroundExecutor = Executors.newSingleThreadScheduledExecutor();
  3. // Execute a task in the background thread.
  4. backgroundExecutor.execute(new Runnable() {
  5. @Override
  6. public void run() {
  7. // Your code logic goes here.
  8. }
  9. });
  10. // Execute a task in the background thread after 1 second.
  11. backgroundExecutor.schedule(new Runnable() {
  12. @Override
  13. public void run() {
  14. // Your code logic goes here
  15. }
  16. }, 1, TimeUnit.SECONDS);

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

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

3.在后台线程中执行代码,并在主线程上更新ui。

  1. // Create an executor that executes tasks in the main thread.
  2. Executor mainExecutor = ContextCompat.getMainExecutor(this);
  3. // Create an executor that executes tasks in a background thread.
  4. ScheduledExecutorService backgroundExecutor = Executors.newSingleThreadScheduledExecutor();
  5. // Execute a task in the background thread.
  6. backgroundExecutor.execute(new Runnable() {
  7. @Override
  8. public void run() {
  9. // Your code logic goes here.
  10. // Update UI on the main thread
  11. mainExecutor.execute(new Runnable() {
  12. @Override
  13. public void run() {
  14. // You code logic goes here.
  15. }
  16. });
  17. }
  18. });

解决方案2:使用以下构造函数之一显式指定循环器。
处理程序(活套)
处理程序(looper,handler.callback)
1.在主线程中执行代码
1.1. 带活套的处理器

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

1.2带循环器和handler.callback的处理程序

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

2.在后台线程中执行代码
2.1. 带活套的处理器

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

2.2. 带有循环器和handler.callback的处理程序

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

注意:使用后记得松开螺纹。

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

3.在后台线程中执行代码,并在主线程上更新ui。

  1. // Create a handler to execute code in the main thread
  2. Handler mainHandler = new Handler(Looper.getMainLooper());
  3. // Create a background thread that has a Looper
  4. HandlerThread handlerThread = new HandlerThread("HandlerThread");
  5. handlerThread.start();
  6. // Create a handler to execute in the background thread
  7. Handler backgroundHandler = new Handler(handlerThread.getLooper(), new Handler.Callback() {
  8. @Override
  9. public boolean handleMessage(@NonNull Message message) {
  10. // Your code logic goes here.
  11. // Update UI on the main thread.
  12. mainHandler.post(new Runnable() {
  13. @Override
  14. public void run() {
  15. }
  16. });
  17. return true;
  18. }
  19. });
展开查看全部

相关问题