Asked  7 Months ago    Answers:  2   Viewed   49 times

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 UDPListenerService extends Service
    private static final String TAG = "UDPListenerService";
    //private ThreadGroup myThreads = new ThreadGroup("UDPListenerServiceWorker");
    private UDPListenerThread myThread;
     * Handler to communicate from WorkerThread to service.
    private Handler mServiceHandler;

    // Used to receive messages from the Activity
    final Messenger inMessenger = new Messenger(new IncomingHandler());
    // Use to send message to the Activity
    private Messenger outMessenger;

    class IncomingHandler extends Handler
        public void handleMessage(Message msg)

     * Target we publish for clients to send messages to Incoming Handler.
    final Messenger mMessenger = new Messenger(new IncomingHandler());
    [ ... ]

And here, final Messenger mMessenger = new Messenger(new IncomingHandler());, I get the following Lint warning:

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

What does it mean?



If IncomingHandler class is not static, it will have a reference to your Service object.

Handler objects for the same thread all share a common Looper object, which they post messages to and read from.

As messages contain target Handler, as long as there are messages with target handler in the message queue, the handler cannot be garbage collected. If handler is not static, your Service or Activity cannot be garbage collected, even after being destroyed.

This may lead to memory leaks, for some time at least - as long as the messages stay int the queue. This is not much of an issue unless you post long delayed messages.

You can make IncomingHandler static and have a WeakReference to your service:

static class IncomingHandler extends Handler {
    private final WeakReference<UDPListenerService> mService; 

    IncomingHandler(UDPListenerService service) {
        mService = new WeakReference<UDPListenerService>(service);
    public void handleMessage(Message msg)
         UDPListenerService service = mService.get();
         if (service != null) {

See this post by Romain Guy for further reference

Tuesday, June 1, 2021
answered 7 Months ago

Make your handler a static class.

The warning is a lint warning. You can disable the warning but its a useful info

Here's a list of Lint Check

Quoting from the source @

Avoid non-static inner classes in an activity if you don't control their life cycle, use a static inner class and make a weak reference to the activity inside.

The solution to this issue is to use a static inner class with a WeakReference to the outer class, as done in ViewRoot and its W inner class for instance.

Also check this discussion on android developers group. Check the solution by Romain Guy!topic/android-developers/1aPZXZG6kWk

Example from Romain Guy's solution from the above link

 class OuterClass { 
 class InnerClass { 
  private final WeakReference<OuterClass> mTarget; 

   InnerClass(OuterClass target) { 
    mTarget = new WeakReference<OuterClass>(target); 

  void doSomething() { 
  OuterClass target = mTarget.get(); 
  if (target != null); 



public class MainActivity extends Activity {

      LinearLayout ll;
    public void onCreate(Bundle savedInstanceState) {
        ll = new LinearLayout(this);
        new HandlerClass(this);
       private static class HandlerClass extends Handler{
           private final WeakReference<MainActivity> mTarget; 
        public HandlerClass(MainActivity context)
             mTarget = new WeakReference<MainActivity>((MainActivity) context);


            public void handleMessage(Message msg) {
                MainActivity target = mTarget.get(); 
                if (target != null) 
                    // anim.stop();

                 String str = (String) msg.obj;
                 TextView tv = new TextView(target);
                 //sv.scrollBy(0, 20);




Correct me if the above is wrong or has some issues.

You can also check this blog by Alex Lockwood

Friday, September 17, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :