您的位置:首页 > 其它

Handler 相关的问题

2015-09-07 17:44 295 查看
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() {@Overridepublic 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.
再看吧。。。
3. 因为只由主线程可以对UI进行修改,所以是不是只由主线程有自己的 Looper,“程序员自己启动的子线程,程序员必须自己创建一个Looper对象,并启动”的意义何在?子线程并不需要Looper来进行UI修改啊,不是也改不了么?是自定义的Activity要这么来做?
的确可以在不需要使用到Looper的时候进行Handler的使用,另外在子线程中使用handler 然后更新UI,还是会导致异常,但是使用 Toast.show() 没有问题。[code]CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: