This Handler class should be static or leaks might occur: IncomingHandler

前端 未结 7 1830
深忆病人
深忆病人 2020-11-22 07:14

I\'m developing an Android 2.3.3 application with a service. I have this inside that service to communicate with Main activity:

public class UDPListenerServi         


        
7条回答
  •  既然无缘
    2020-11-22 07:22

    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 Handler and there is no Handler non-static inner class):

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

    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.

提交回复
热议问题