`
coding1688
  • 浏览: 232266 次
  • 来自: 上海
社区版块
存档分类
最新评论

常被忽略的警告:This Handler class should be static or leaks might occur

 
阅读更多

在Android开发中,使用多线程处理时,如果需要通知界面需要用到Handler机制,如果不注意就会报如下警告信息:

This Handler class should be static or leaks might occur

在StackOverflow上有这样一篇文章讲到了这个问题(感谢何海英):

http://stackoverflow.com/questions/11407943/this-handler-class-should-be-static-or-leaks-might-occur-incominghandler

 

 

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 WeakRference<UDPListenerService>(service);
    }
    @Override
    public void handleMessage(Message msg)
    {
         UDPListenerService service = mService.get();
         if (service != null) {
              service.handleMessage(msg);
         }
    }
}

 

0
2
分享到:
评论

相关推荐

    Android Handler leak分析及解决办法详解

     In Android, Handler classes should be static or leaks might occur, Messages enqueued on the application thread’s MessageQueue also retain their target Handler. If the Handler is an inner class, its...

    进度条对话框:通过Handler实现

    NULL 博文链接:https://andli.iteye.com/blog/1489903

    Pro Android学习:了解Handler小例子

    这是Pro Android学习系列中Android Service部分的例子源代码。相关学习笔记见:http://blog.csdn.net/flowingflying/article/details/6212512

    handler的使用案例

    demo01: 通过handler.post()的方法可以更新ui demo02: 一秒钟切换一张图片 demo03: 霓虹灯动态效果的显示 demo04: 倒计时,停止倒计时 demo05: 通过handler.sendMessage和handleMessage发送处理消息 demo06: message...

    netty-handler-4.1.65.Final-API文档-中英对照版.zip

    Maven坐标:io.netty:netty-handler:4.1.65.Final; 标签:netty、handler、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中...

    Sortable前端框架

    This option sets the minimum pointer movement that must occur before the delayed sorting is cancelled. Values between 3 to 5 are good. --- #### `disabled` options Disables the sortable if set to ...

    netty-handler-4.1.73.Final-API文档-中文版.zip

    Maven坐标:io.netty:netty-handler:4.1.73.Final; 标签:handler、netty、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的...

    Android源代码:HandlerDemo

    Android源代码:HandlerDemo。。。。。。。。。。。。

    Class-ErrorHandler

    Class::ErrorHandler - 错误处理的基类 概要 package Foo; use base qw( Class::ErrorHandler ); sub class_method { my $class = shift; ... return $class-&gt;error("Help!") unless $continue; } sub object_...

    netty-handler-4.1.24.Final-API文档-中文版.zip

    Maven坐标:io.netty:netty-handler:4.1.24.Final; 标签:handler、netty、jar包、java、API文档、中文版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档...

    netty-handler-4.1.68.Final-API文档-中文版.zip

    Maven坐标:io.netty:netty-handler:4.1.68.Final; 标签:netty、handler、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的...

    netty-handler-4.1.65.Final-API文档-中文版.zip

    Maven坐标:io.netty:netty-handler:4.1.65.Final; 标签:netty、handler、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的...

    netty-handler-4.1.74.Final-API文档-中文版.zip

    Maven坐标:io.netty:netty-handler:4.1.74.Final; 标签:netty、handler、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的...

    netty-handler-proxy-4.1.68.Final-API文档-中文版.zip

    赠送jar包:netty-handler-proxy-4.1.68.Final.jar; 赠送原API文档:netty-handler-proxy-4.1.68.Final-javadoc.jar; 赠送源代码:netty-handler-proxy-4.1.68.Final-sources.jar; 赠送Maven依赖信息文件:netty-...

    netty-handler-proxy-4.1.73.Final-API文档-中文版 (1).zip

    赠送jar包:netty-handler-proxy-4.1.73.Final.jar; 赠送原API文档:netty-handler-proxy-4.1.73.Final-javadoc.jar; 赠送源代码:netty-handler-proxy-4.1.73.Final-sources.jar; 赠送Maven依赖信息文件:netty-...

    netty-handler-proxy-4.1.73.Final-API文档-中英对照版.zip

    赠送jar包:netty-handler-proxy-4.1.73.Final.jar; 赠送原API文档:netty-handler-proxy-4.1.73.Final-javadoc.jar; 赠送源代码:netty-handler-proxy-4.1.73.Final-sources.jar; 赠送Maven依赖信息文件:netty-...

    UIAleraction

    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]; //确认按钮 UIAlertAction *other = [UIAlertAction ...

    netty-handler-4.1.11.Final-API文档-中文版.zip

    Maven坐标:io.netty:netty-handler:4.1.11.Final; 标签:handler、netty、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的...

    各种警告框源码

    各种警告框源码

    Handler与Android多线程详解

    下面是一段大家都比较熟悉的代码: 代码如下:Handler handler = new Handler(); handler.post(myThread); //使用匿名内部类创建一个线程myThreadRunnable mythread = new Runnable() {public void run() {}};一开始...

Global site tag (gtag.js) - Google Analytics