随手写个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使用有警告;的更多相关文章

  1. Android下setLatestEventInfo警告、Handler警告、SimpleDateFormat警告

    正 文: 今天飘易在做Android 4.4.2下的APP开发时,使用了Notification下的setLatestEventInfo()方法时,Eclipse却提示:“ 不建议使用类型 Notif ...

  2. Android Handler警告,SimpleDateFormat警告

    1:Handler// This Handler class should be static or leaks might occur: IncomingHandler    @SuppressLi ...

  3. android 的几个黄色警告解决办法(转)

    转自:http://my.eoe.cn/864234/archive/5162.html 1:Handler 1 2 3 4 5 6 7 8 // This Handler class should ...

  4. 倒计时 总结 Timer Handler CountDownTimer RxJava MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  5. 【技术博客】利用handler实现线程之间的消息传递

    [技术博客]利用handler实现线程之间的消息传递 一.handler简介 在Android Studio的开发中,经常需要启动多个线程.比如向远程发送请求时,必须新开一个子线程,否则会造成程序崩溃 ...

  6. 【Android 应用开发】 自定义组件 宽高适配方法, 手势监听器操作组件, 回调接口维护策略, 绘制方法分析 -- 基于 WheelView 组件分析自定义组件

    博客地址 : http://blog.csdn.net/shulianghan/article/details/41520569 代码下载 : -- GitHub : https://github.c ...

  7. Android学习笔记_69_android 支付宝之网页支付和快捷支付

    参考资料: https://b.alipay.com/order/productDetail.htm?productId=2013080604609654 https://b.alipay.com/o ...

  8. Android 接入支付宝支付实现

    接上篇android接入微信支付文章,这篇我们带你来接入支付宝支付服务 简介 首先要说明的是个人感觉接入支付宝比微信简单多了,很轻松的,所以同学们不要紧张~ 当然还是老规矩啦,上来肯定的贴上官网地址, ...

  9. Android接入支付宝支付实现

    接上篇android接入微信支付文章,这篇我们带你来接入支付宝支付服务 简介 首先要说明的是个人感觉接入支付宝比微信简单多了,很轻松的,所以同学们不要紧张~ 当然还是老规矩啦,上来肯定的贴上官网地址, ...

随机推荐

  1. js获取谷歌浏览器版本

    根据浏览器的useragent获取浏览器信息 // 获取谷歌浏览器版本 function getChromeVersion() { var arr = navigator.userAgent.spli ...

  2. learning makefile = and :=

  3. js中通过Object.prototype.toString方法----精确判断对象的类型

    判断是否为函数 function isFunction(it) {        return Object.prototype.toString.call(it) === '[object Func ...

  4. Python *Mix_w7

    1. str中的join方法. 把列表转换成字符串 g = ["中国", "美国", "韩国", "法国"] s = & ...

  5. 使用matlab生成用于ROM初始化的coe文件(转)

    reference:https://www.cnblogs.com/chensimin1990/p/9759368.html t=0:2*pi/2^12:2*pi; y=0.5*sin(t)+0.5; ...

  6. Python中异常处理

    高级语言通常都配置了一套try...except...finally的错误处理机制. 1.我们先看一个try的机制 try: res=1/0except ZeroDivisionError as e: ...

  7. 马凯军201771010116《面向对象程序设计(java)》第一周学习总结

    马凯军201771010116<面向对象程序设计(java)>第一周学习总结 第一部分:课程准备部分 填写课程学习 平台注册账号, 平台名称 注册账号 博客园:www.cnblogs.co ...

  8. 2017年4月7日16:18:17 java8 常用记录

    List<String> customerUids = customerTagModel.stream().map(CustomerTagModel::getCustomerUid)   ...

  9. 关于手贱--npm 误改全局安装路径

    1.通过 npm config set prefix "目录路径" 来设置.通过 npm config get prefix 来获取当前设置的目录. 2.npm config se ...

  10. 关于windows下的makefile学习

    文中部分引用自:http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=408225 windows下makefile环境配置见于:http ...