This Handler class should be static or leaks might occur Android
首先解释下这句话This Handler class should be static or leaks might occur,大致意思就是说:Handler类应该定义成静态类,否则可能导致内存泄露。
具体如何解决,在国外有人提出,如下:
Issue: Ensures that Handler classes do not hold on to a reference to an outer class
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 outer class will be retained as well. To avoid leaking the outer class, declare the Handler as a static nested class with a WeakReference to its outer class.
大体翻译如下:
Handler 类应该应该为static类型,否则有可能造成泄露。在程序消息队列中排队的消息保持了对目标Handler类的应用。如果Handler是个内部类,那 么它也会保持它所在的外部类的引用。为了避免泄露这个外部类,应该将Handler声明为static嵌套类,并且使用对外部类的弱应用。
使用范例:
- static
class MyHandler extends Handler { - WeakReference<PopupActivity> mActivity;
- MyHandler(PopupActivity activity) {
- mActivity = new WeakReference<PopupActivity>(activity);
- }
- @Override
- public
void handleMessage(Message msg) { - PopupActivity theActivity = mActivity.get();
- switch (msg.what) {
- case 0:
- theActivity.popPlay.setChecked(true);
- break;
- }
- }
- };
- MyHandler ttsHandler = new MyHandler(this);
- private Cursor mCursor;
- private
void test() { - ttsHandler.sendEmptyMessage(0);
- }
原文:http://www.cnblogs.com/savagemorgan/archive/2013/01/23/2872371.html
疑问:是否有其它解决方法?
这个提示就是由于Handler的直接引用会导致相关的Activity、Service等无法被GC。如果这么弱应用的话,会出现空指针,有其它解决方法?
抽时间研究下。
==================================================================================================================================
原始代码:
- public
class MainActivity extends Activity { - private
static
int urlIndex = 0; - private
final
static String TAG = MainActivity.class.getSimpleName(); - private
static
final String[] url = { - "http://vdn.apps.cntv.cn/api/getLiveUrlCommonRedirectApi.do?channel=pa://cctv_p2p_hdcctv1&type=ipad",
- "http://74.82.62.53:1935/liverepeater/13.stream/playlist.m3u8", "http://rtmp.cntv.lxdns.com/live/cctv3/playlist.m3u8", };
- private
static
final
int MSG_PLAY = 100; - private
static
final
int MSG_RUN_ADB = 101; - Handler playHandler = new Handler() {
- @Override
- public
void handleMessage(Message msg) { - switch (msg.what) {
- case MSG_PLAY:
- urlIndex = urlIndex > url.length - 1 ? 0 : urlIndex;
- videoView.setVideoPath(url[urlIndex]);
- ++urlIndex;
- break;
- case MSG_RUN_ADB:
- killMediaServer();
- break;
- }
- }
- };
- @Override
- protected
void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- HHVideoView.create();
- setContentView(R.layout.activity_main);
- videoView = ((HHVideoView) findViewById(R.id.videoView));
- videoView.setOnPreparedListener(mPreparedListener);
- videoView.setOnCompletionListener(mCompletionListener);
- videoView.setOnErrorListener(mOnErrorListener);
- playHandler.sendEmptyMessage(MSG_PLAY);
- }
- private HHVideoView videoView = null;
- private MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {
- public
void onPrepared(MediaPlayer paramMediaPlayer) { - // playerHandler.sendEmptyMessage(uiAction.MEDIAPLAYER_ONPREPAREED);
- videoView.start();
- }
- };
- private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
- public
void onCompletion(MediaPlayer paramMediaPlayer) { - }
- };
- private MediaPlayer.OnErrorListener mOnErrorListener = new MediaPlayer.OnErrorListener() {
- public
boolean onError(MediaPlayer paramMediaPlayer, int paramInt1, int paramInt2) { - return
false; - }
- };
- @Override
- public
boolean onCreateOptionsMenu(Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present.
- return
true; - }
- public
boolean onKeyDown(int keyCode, KeyEvent event) { - if (event.getAction() == KeyEvent.ACTION_DOWN) {
- switch (keyCode) {
- case KeyEvent.KEYCODE_0:
- playHandler.sendEmptyMessage(MSG_RUN_ADB);
- break;
- case KeyEvent.KEYCODE_DPAD_DOWN:
- case KeyEvent.KEYCODE_DPAD_UP:
- playHandler.sendEmptyMessage(MSG_PLAY);
- break;
- }
- }
- return
super.onKeyDown(keyCode, event); - }
- }
修改后的代码:
- //activity code
- public
class MainActivity extends Activity { - private
static
int urlIndex = 0; - private
final
static String TAG = MainActivity.class.getSimpleName(); - private
static
final String[] url = { - "http://vdn.apps.cntv.cn/api/getLiveUrlCommonRedirectApi.do?channel=pa://cctv_p2p_hdcctv1&type=ipad",
- "http://74.82.62.53:1935/liverepeater/13.stream/playlist.m3u8", "http://rtmp.cntv.lxdns.com/live/cctv3/playlist.m3u8", };
- PlayHandler playHandler ;
- @Override
- protected
void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- HHVideoView.create();
- setContentView(R.layout.activity_main);
- videoView = ((HHVideoView) findViewById(R.id.videoView));
- videoView.setOnPreparedListener(mPreparedListener);
- videoView.setOnCompletionListener(mCompletionListener);
- videoView.setOnErrorListener(mOnErrorListener);
- playHandler.sendEmptyMessage(PlayHandler.MSG_PLAY);
- }
- private HHVideoView videoView = null;
- private MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {
- public
void onPrepared(MediaPlayer paramMediaPlayer) { - // playerHandler.sendEmptyMessage(uiAction.MEDIAPLAYER_ONPREPAREED);
- videoView.start();
- }
- };
- private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
- public
void onCompletion(MediaPlayer paramMediaPlayer) { - }
- };
- private MediaPlayer.OnErrorListener mOnErrorListener = new MediaPlayer.OnErrorListener() {
- public
boolean onError(MediaPlayer paramMediaPlayer, int paramInt1, int paramInt2) { - return
false; - }
- };
- @Override
- public
boolean onCreateOptionsMenu(Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present.
- return
true; - }
- public
boolean onKeyDown(int keyCode, KeyEvent event) { - if (event.getAction() == KeyEvent.ACTION_DOWN) {
- switch (keyCode) {
- case KeyEvent.KEYCODE_0:
- playHandler.sendEmptyMessage(PlayHandler.MSG_RUN_ADB);
- break;
- case KeyEvent.KEYCODE_DPAD_DOWN:
- case KeyEvent.KEYCODE_DPAD_UP:
- playHandler.sendEmptyMessage(PlayHandler.MSG_PLAY);
- break;
- }
- }
- return
super.onKeyDown(keyCode, event); - }
- public
void setVideoPath() { - urlIndex = urlIndex > url.length - 1 ? 0 : urlIndex;
- videoView.setVideoPath(url[urlIndex]);
- ++urlIndex;
- }
- }
Handler代码:
- //handler code
- import java.lang.ref.WeakReference;
- import android.os.Handler;
- import android.os.Message;
- /**
- * @author jevan
- * @version (1.0 at 2013-7-3)
- *
- */
- public
class PlayHandler extends Handler { - public
static
final
int MSG_PLAY = 100; - public
static
final
int MSG_RUN_ADB = 101; - WeakReference<MainActivity> mActivity;
- PlayHandler(MainActivity activity) {
- mActivity = new WeakReference<MainActivity>(activity);
- }
- @Override
- public
void handleMessage(Message msg) { - MainActivity theActivity = mActivity.get();
- if(theActivity == null)
- return;
- switch (msg.what) {
- case MSG_PLAY:
- theActivity.setVideoPath();
- break;
- case MSG_RUN_ADB:
- break;
- }
- }
- }
个人还是倾向使用独立的Handler(也就是那个外国人的解决方案),上面反映的Activity会被gc掉,导致参数空指针的问题,其实不能算问题。如果Activity被回收掉,那么Handler应该在使用之前对其状态进行判断。
个人推荐这个解决方法,当然代码会多两行。
This Handler class should be static or leaks might occur Android的更多相关文章
- Android“This Handler class should be static or leaks might occur”警告的处理方法
此文属于转载! 最近用到handle在线程中改变UI,会跟给出“This Handler class should be static or leaks might occur”的警告,网上看了很多解 ...
- Android - This Handler class should be static or leaks might occur.
今天学习了使用 HTTP协议,从Android客户端往Tomcat服务器端以GET发送请求,途中无意中发现自己写的Handler类被Android提示:This Handler class shoul ...
- This Handler class should be static or leaks might occur(null) 解决办法 (转)
原文链接:http://blog.csdn.net/wuleihenbang/article/details/17126371 首先解释下这句话This Handler class should be ...
- android之lint警告This Handler class should be static or leaks might occur
更新到adt2.0的开发者们可能会在handler上发现这么一条警告:This Handler class should be static or leaks might occur . 首先在ADT ...
- This Handler class should be static or leaks might occur,Handler和Context使用的注意事项!
Android中.在使用到Handler的时候,假设按例如以下代码编写: private Handler handler; @Override public void onCreate(Bundle ...
- Handler classes should be static or leaks might occur
http://droidyue.com/blog/2014/12/28/in-android-handler-classes-should-be-static-or-leaks-might-occur ...
- 85、android handler的警告Handler Class Should be Static or Leaks Occur
转载:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1106/1922.html 在使用Handler更新UI的时候,我是这样写 ...
- 【Android】[转] Android Handler应设为static
android开发中,使用Lint检测时会提示这么一句话 : This Handler class should be static or leaks might occur.意为handler应用s ...
- Handler Should be static or leaks Occur?
解决办法: public class SampleActivity extends Activity { /** * Instances of static inner classes do not ...
随机推荐
- WebApi接口传参不再困惑:传参详解
原文出处: 懒得安分 前言:还记得刚使用WebApi那会儿,被它的传参机制折腾了好久,查阅了半天资料.如今,使用WebApi也有段时间了,今天就记录下API接口传参的一些方式方法,算是一个笔记,也希望 ...
- 微软MSBI商业智能视频
第一讲:BI介质安装.BI基础知识讲解.BI方法论讲解.项目讲解 1.BI基础知识讲解.BI方法论实施2.微软BI的介绍(数据仓库介绍.SSIS介绍.SSAS介绍.SS ...
- 读书笔记 effective c++ Item 10 让赋值运算符返回指向*this的引用
一个关于赋值的有趣的事情是你可以将它们链在一起: int x, y, z; x = y = z = ; // chain of assignments 同样有趣的是赋值采用右结合律,所以上面的赋值链被 ...
- TypeScript 优秀开源项目大合集
TypeScript出来有段时间了,也冒出了很多用TypeScript开发的优秀开源项目,搜寻了一些基于TypeScript项目,分享给大家: https://github.com/brookshi/ ...
- 对spring web启动时IOC源码研究
研究IOC首先创建一个简单的web项目,在web.xml中我们都会加上这么一句 <context-param> <param-name>contextConfigLocatio ...
- inline元素的margin与padding
替换元素与非替换元素 替换元素(replaced element):所谓替换元素就是浏览器根据元素的标签和属性,来决定元素具体显示什么内容.比如说:img标签的src属性的值用来读取图片信息并且显示出 ...
- 4001: [TJOI2015]概率论
4001: [TJOI2015]概率论 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 262 Solved: 108[Submit][Status] ...
- 3997: [TJOI2015]组合数学
3997: [TJOI2015]组合数学 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 247 Solved: 174[Submit][Status ...
- 免费瘫软入院,付费发飙成壮汉,YoMail 想干嘛?
大家好,我是YoMail 最近,Yo妹在思考一个非常严肃的事情. YoMail 全新升级,开启会员style! 新版叫Membership,即日就要与大家见面. 他的与众不同是推出"会员 ...
- Rabbitmq 性能测试
背景: 线上环境,出了一起事故,初步定位是rabbitmq server. 通过抓包发现,是有多个应用使用同一台rabbitmq server.并且多个应用使用rabbitmq的方式也不一样.发现有以 ...