【Android】[转] Android Handler应设为static

android开发中,使用Lint检测时会提示这么一句话 : This Handler class should be static or leaks might occur。意为handler应用static修饰否则容易发生内存泄漏。


ADT20有这么一个变化:Look for handler leaks: This check makes sure that a handler inner class does not hold an implicit reference to its outer class。意为handler不应包括外部类的隐式引用。


扩展开来就是:同一个线程下的handler共享一个looper对象,消息中保留了对handler的引用,只要有消息在队列中,那么handler便无法被回收,如果handler不是static,那么使用Handler的Service和Activity就也无法被回收,即便它们的ondestroy方法被调用。这就可能导致内存泄露。当然这通常不会发生,除非你发送了一个延时很长的消息。


但把hanlder添加为static后,会发现在handler中调用外部类的方法和成员变量需要它们都定义为final,这显然是不大可能的。这里建议在你的Service或Activity中的增加一个内部static Handler类,这个内部类持有Service或Activity的弱引用,这样就可以解决final的问题。

static class IncomingHandler extends Handler {  
    private final WeakReference<UDPListenerService> mService;  
   
    IncomingHandler(UDPListenerService service) {  
        mService = new WeakRference<UDPListenerService>(service);  
    }  
    @Override  
    public void handleMessage(Message msg)  
    {  
         UDPListenerService service = mService.get();  
         if (service != null) {  
              service.handleMessage(msg);  
         }  
    }  
}  

 

static class MyHandler extends Handler {  
                WeakReference<PopupActivity> mActivity;  
  
                MyHandler(PopupActivity activity) {  
                        mActivity = new WeakReference<PopupActivity>(activity);  
                }  
  
                @Override  
                public void handleMessage(Message msg) {  
                        PopupActivity theActivity = mActivity.get();  
                        switch (msg.what) {  
                        case 0:  
                                theActivity.popPlay.setChecked(true);  
                                break;  
                        }  
                }  
        };  
  
        MyHandler ttsHandler = new MyHandler(this);  
        private Cursor mCursor;  
  
        private void test() {  
                ttsHandler.sendEmptyMessage(0);  
        }  
}  
static class MHandler extends Handler {  
    WeakReference<OuterClass> outerClass;  
  
    MHandler(OuterClass activity) {  
        outerClass = new WeakReference<OuterClass>(activity);  
    }  
  
    @Override  
    public void handleMessage(android.os.Message msg) {  
        OuterClass theClass = outerClass.get();  
        switch (msg.what) {  
        case 0: {  
            //使用theClass访问外部类成员和方法  
            break;  
        }  
        default: {  
            Log.w(TAG, "未知的Handler Message:" + msg.what);  
        }  
        }  
  
    }  
}  

 

你可能感兴趣的:(【Android】[转] Android Handler应设为static)