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 constructingHandler
(i.e. you don't subclassHandler
and there is noHandler
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 themIncomingHandler
object which holds a reference theHandler.Callback
object which holds a reference to theService
object. As long as there are messages in theLooper
message queue, theService
will not be GC. However, it won't be a serious issue unless you have long delay messages in the message queue.
handler 警告This Handler class should be static or leaks might occur 的回调解决方法
最新推荐文章于 2021-07-16 17:05:01 发布