1. Android Studio 不能自动 import class android.os.Handler,而是直接 import class java.util.logging.Handler,这是什么鬼?难道是因为 Handler 实例不能直接 new?
似乎是 Android Studio 的一个缺陷,手动 import 吧
2. Android Studio 黄色警告,可能会有泄漏,这个怎么处理?
final android.os.Handler myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
// do sth
}
super.handleMessage(msg);
}
};
Handler reference leaks
Since this Handler is declared as an inner class, it may prevent the outer class from being garbage collected.
If the Handler is using a Looper or MessageQueue for a thread other than the main thread, then there is no issue.
If the Handler is using the Looper or MessageQueue of the main thread, you need to fix your Handler declaration, as follows:
Declare the Handler as a static class;
In the outer class, instantiate a WeakReference to the outer class and pass this object to your Handler when you instantiate the Handler;
Make all references to members of the outer class using the WeakReference object.