在Android开发中,使用多线程处理时,如果需要通知界面需要用到Handler机制,如果不注意就会报如下警告信息:
在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);
}
}
}
分享到:
相关推荐
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...
NULL 博文链接:https://andli.iteye.com/blog/1489903
这是Pro Android学习系列中Android Service部分的例子源代码。相关学习笔记见:http://blog.csdn.net/flowingflying/article/details/6212512
demo01: 通过handler.post()的方法可以更新ui demo02: 一秒钟切换一张图片 demo03: 霓虹灯动态效果的显示 demo04: 倒计时,停止倒计时 demo05: 通过handler.sendMessage和handleMessage发送处理消息 demo06: message...
Maven坐标:io.netty:netty-handler:4.1.65.Final; 标签:netty、handler、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中...
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 ...
Maven坐标:io.netty:netty-handler:4.1.73.Final; 标签:handler、netty、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的...
Android源代码:HandlerDemo。。。。。。。。。。。。
Class::ErrorHandler - 错误处理的基类 概要 package Foo; use base qw( Class::ErrorHandler ); sub class_method { my $class = shift; ... return $class->error("Help!") unless $continue; } sub object_...
Maven坐标:io.netty:netty-handler:4.1.24.Final; 标签:handler、netty、jar包、java、API文档、中文版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档...
Maven坐标:io.netty:netty-handler:4.1.68.Final; 标签:netty、handler、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的...
Maven坐标:io.netty:netty-handler:4.1.65.Final; 标签:netty、handler、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的...
Maven坐标:io.netty:netty-handler:4.1.74.Final; 标签:netty、handler、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的...
赠送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-...
赠送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-...
赠送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-...
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]; //确认按钮 UIAlertAction *other = [UIAlertAction ...
Maven坐标:io.netty:netty-handler:4.1.11.Final; 标签:handler、netty、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的...
各种警告框源码
下面是一段大家都比较熟悉的代码: 代码如下:Handler handler = new Handler(); handler.post(myThread); //使用匿名内部类创建一个线程myThreadRunnable mythread = new Runnable() {public void run() {}};一开始...