android中的构造方法,Android 11推荐使用Handler的构造方法

背景

Android 11(即API 30:Android R)弃用了Handler默认的无参构造方法

082ff6949ead23a5b6d709399e75b853.png

源代码(android.os.Handler)

//API 30,Android 11

/**

* Default constructor associates this handler with the {@link Looper} for the

* current thread.

*

* If this thread does not have a looper, this handler won't be able to receive messages

* so an exception is thrown.

*

* @deprecated Implicitly choosing a Looper during Handler construction can lead to bugs

* where operations are silently lost (if the Handler is not expecting new tasks and quits),

* crashes (if a handler is sometimes created on a thread without a Looper active), or race

* conditions, where the thread a handler is associated with is not what the author

* anticipated. Instead, use an {@link java.util.concurrent.Executor} or specify the Looper

* explicitly, using {@link Looper#getMainLooper}, {link android.view.View#getHandler}, or

* similar. If the implicit thread local behavior is required for compatibility, use

* {@code new Handler(Looper.myLooper())} to make it clear to readers.

*

*/

@Deprecated

public Handler() {

this(null, false);

}

原因:

@deprecated Implicitly choosing a Looper during Handler construction can lead to bugs

隐式的Looper会导致一些错误(场景如下)

where operations are silently lost (if the Handler is not expecting new tasks and quits),

操作丢失

crashes (if a handler is sometimes created on a thread without a Looper active), or race conditions,

程序崩溃和紊乱情况

where the thread a handler is associated with is not what the author anticipated.

非期望的Handler

推荐使用方式

使用Executor接口或者是明确指定Looper

use an {@link java.util.concurrent.Executor} or specify the

Looper explicitly

使用Looper静态方法getMainLooper()或View实例方法getHandler()

using {@link Looper#getMainLooper}, {link

android.view.View#getHandler}, or similar.

方法源代码

//android.os.Looper

public final class Looper{

//...

/**

* Returns the application's main looper, which lives in the main thread of the application.

*/

public static Looper getMainLooper() {

synchronized (Looper.class) {

return sMainLooper;

}

}

}

//android.view.View

@UiThread

public class View implements Drawable.Callback, KeyEvent.Callback,

AccessibilityEventSource {

//....

/**

* @return A handler associated with the thread running the View. This

* handler can be used to pump events in the UI events queue.

*/

public Handler getHandler() {

final AttachInfo attachInfo = mAttachInfo;

if (attachInfo != null) {

return attachInfo.mHandler;

}

return null;

}

}

例子

//Looper的静态方法getMainLooper()作为Handler构造方法的参数

private Handler looperHandler = new Handler(Looper.getMainLooper());

//view的getHandler()方法必须先存在实例对象,方法直接返回Handler对象

//@param view View的实例对象

Handler viewHandler = view.getHandler();

使用Looper的静态方法myLooper()

If the implicit thread local behavior is required for compatibility, use {@code new Handler(Looper.myLooper())} to make it clear to readers.

方法源代码:

//android.os.Looper

/**

* Return the Looper object associated with the current thread. Returns

* null if the calling thread is not associated with a Looper.

*/

public static @Nullable Looper myLooper() {

return sThreadLocal.get();

}

例子:

private Handler handler = new Handler(Looper.myLooper());

本文地址:https://blog.csdn.net/qq_38666896/article/details/108990831

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值