@SuppressLint("HandlerLeak"),或Handler使用有警告;
随手写个Handler,然后飘黄,看着挺难受(黄色警告的大概意思:Handler可能会内存泄漏,推荐你用静态内部类+实例化弱引用);
This Handler class should be static or leaks might occur (anonymous android.os.Handler) less...
Since this Handler is declared as an inner class, it may prevent the outer class from being garbage collected. If the Handler is using a Looper or MessageQueue for a thread other than the main thread, then there is no issue. If the Handler is using the Looper or MessageQueue of the main thread, you need to fix your Handler declaration, as follows: Declare the Handler as a static class; In the outer class, instantiate a WeakReference to the outer class and pass this object to your Handler when you instantiate the Handler; Make all references to members of the outer class using the WeakReference object.
这个处理程序类应该是静态的,否则可能会发生泄漏(android.os.Handler)
由于这个处理程序声明为内部类,它可能会阻止外部类被垃圾收集。如果处理程序对主线程以外的线程使用循环程序或MessageQueue,则没有问题。如果处理程序使用主线程的Looper或MessageQueue,则需要修复处理程序声明,如下所示:将处理程序声明为静态类;在外部类中,实例化对外部类的弱引用,并在实例化处理程序时将此对象传递给处理程序;使用WeakReference对象引用外部类的所有成员。
不管,先提示一下:
这个注解看着也难受;
按照它的警告修改一下(推荐使用):
//静态内部类 弱引用
private BleHandler mHandler = new BleHandler(getApplication());
static class BleHandler extends Handler{
WeakReference weakReference;
public BleHandler(Context context){
weakReference = new WeakReference(context);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (weakReference == null)return;
}
}
还可以这样去写,没有警告:
private Handler mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message message) {
return false;
}
});
@SuppressLint("HandlerLeak"),或Handler使用有警告;的更多相关文章
- Android下setLatestEventInfo警告、Handler警告、SimpleDateFormat警告
正 文: 今天飘易在做Android 4.4.2下的APP开发时,使用了Notification下的setLatestEventInfo()方法时,Eclipse却提示:“ 不建议使用类型 Notif ...
- Android Handler警告,SimpleDateFormat警告
1:Handler// This Handler class should be static or leaks might occur: IncomingHandler @SuppressLi ...
- android 的几个黄色警告解决办法(转)
转自:http://my.eoe.cn/864234/archive/5162.html 1:Handler 1 2 3 4 5 6 7 8 // This Handler class should ...
- 倒计时 总结 Timer Handler CountDownTimer RxJava MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 【技术博客】利用handler实现线程之间的消息传递
[技术博客]利用handler实现线程之间的消息传递 一.handler简介 在Android Studio的开发中,经常需要启动多个线程.比如向远程发送请求时,必须新开一个子线程,否则会造成程序崩溃 ...
- 【Android 应用开发】 自定义组件 宽高适配方法, 手势监听器操作组件, 回调接口维护策略, 绘制方法分析 -- 基于 WheelView 组件分析自定义组件
博客地址 : http://blog.csdn.net/shulianghan/article/details/41520569 代码下载 : -- GitHub : https://github.c ...
- Android学习笔记_69_android 支付宝之网页支付和快捷支付
参考资料: https://b.alipay.com/order/productDetail.htm?productId=2013080604609654 https://b.alipay.com/o ...
- Android 接入支付宝支付实现
接上篇android接入微信支付文章,这篇我们带你来接入支付宝支付服务 简介 首先要说明的是个人感觉接入支付宝比微信简单多了,很轻松的,所以同学们不要紧张~ 当然还是老规矩啦,上来肯定的贴上官网地址, ...
- Android接入支付宝支付实现
接上篇android接入微信支付文章,这篇我们带你来接入支付宝支付服务 简介 首先要说明的是个人感觉接入支付宝比微信简单多了,很轻松的,所以同学们不要紧张~ 当然还是老规矩啦,上来肯定的贴上官网地址, ...
随机推荐
- python常见错误
最近刚刚接触Python,为了养成好习惯,遇到了诸多的问题,林林总总,在这里简单记录下: 编写简单的python语句时: module level import not at top of file ...
- qssp2017 source
ftp://ftp.gfz-potsdam.de/pub/home/turk/wang
- SharePoint REST API - 使用REST接口对列表设置自定义权限
博客地址:http://blog.csdn.net/FoxDave SharePoint网站.列表和列表项都属于SecurableObject类型.默认情况下,一个安全对象继承父级的权限.对一个对 ...
- void的几点用法
1.可以通过void 0 获取undefined.等同于void(0). void 任意数 === undefined // true void(0) === undefined // true ...
- <zk在大型分布式系统中的应用>
Hadoop 在hadoop中,zk主要用来实现HA(High Availability).这部分逻辑主要集中在hadoop common的HA模块中,HDFS的NameNode和Yarn的Resou ...
- Linux 驱动——从宏观上掌握基本框架
一.一个简单的驱动程序实例 led_drv.c 驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include ...
- linux权限相关操作
Linux权限管理是Linux中一个十分重要的概念,也是系统安全性的重要保障.这里主要介绍Linux的基本权限和默认权限,通过理论讲解与实验演示,可以详细了解到权限的相关操作及其重要性. 文件权限 [ ...
- MySQL8.0 关闭二进制日志
Binary logging is enabled by default (the log_bin system variable is set to ON). The exception is if ...
- Jekins学习
1.Windows 安装入门 https://blog.csdn.net/u011541946/article/details/78003772 PS:坑1,https问题 坑2,offline ...
- redis使用get key中文变成十六进制编码
redis-cli 后面加上 --raw 解决中文显示问题 redis-cli -h 127.0.0.1 -p 端口 -a 密码 --raw 不带 --raw 参数: redis-cli -h &g ...