@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笔记15-集合
集合也是一种数据类型,一个类似列表东西,它的特点是无序的,不能根据下标来取值,不重复的,也就是说集合中是没有重复的数据 #定义集合:# #方法1:nums = [1,1,1,1,23,4,56]#定义 ...
- JAVA数据库操作回滚小结
一:总结的原因 在最近的工作中,遇到了一个一对多关系多表数据传输,传送成功状态绑定在主数据表上,因为代码不健壮问题造成了主表传送状态更新失败,而子表数据就被重复插入.又由于数据传输频率很高,我们的测试 ...
- web.xml配置以及一些详解
web.xml的根元素定义如下所示(代表当前使用哪个模版): <?xml version="1.0" encoding="UTF-8"?> < ...
- TensorFlow函数:tf.random_shuffle
tf.random_shuffle 函数 random_shuffle( value, seed=None, name=None ) 定义在:tensorflow/python/ops/random_ ...
- datePecker时间控件区间写法
成交时间: <input type="text" onclick="WdatePicker({dateFmt:'yyyy-MM-dd',maxDate:'#F{$d ...
- spark on yarn运行产生jar包冲突问题
1.1 问题描述 Spark Streaming程序解析protobuf序列化的数据时,--jars 来添加依赖的protobuf-java-3.0.0.jar包,使用local模式程序正常,使用ya ...
- HTML前序
HTML基本格式 <!DOCTYPE html> //文档类型声明 <html lang="zh-cn"> //表示HTML文档开始,属性lang,属性值= ...
- 学习笔记TF043:TF.Learn 机器学习Estimator、DataFrame、监督器Monitors
线性.逻辑回归.input_fn()建立简单两个特征列数据,用特证列API建立特征列.特征列传入LinearClassifier建立逻辑回归分类器,fit().evaluate()函数,get_var ...
- too many open files(打开的文件过多)解决方法
https://blog.csdn.net/roy_70/article/details/78423880 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.n ...
- cordova网络情况检测插件使用:cordova-plugin-network-information
1. 添加插件 : cordova plugin add cordova-plugin-network-information 2. 调用方法: document.addEventListener(' ...