handler 警告This Handler class should be static or leaks might occur 的回调解决方法

来源:互联网 发布:ubuntu 断电日志 编辑:程序博客网 时间:2024/05/20 04:29

As others have mentioned the Lint warning is because of the potential memory leak. You can avoid the Lint warning by passing a Handler.Callback when constructing Handler (i.e. you don't subclass Handlerand there is no Handler non-static inner class):

Handler mIncomingHandler = new Handler(new Handler.Callback() {    @Override    public boolean handleMessage(Message msg) {    }});

As I understand it, this will not avoid the potential memory leak. Message objects hold a reference to the mIncomingHandler object which holds a reference the Handler.Callback object which holds a reference to the Service object. As long as there are messages in the Looper message queue, the Service will not be GC. However, it won't be a serious issue unless you have long delay messages in the message queue.

0 0
原创粉丝点击