首先解釋下這句話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應該在使用之前對其狀態進行判斷。
個人推薦這個解決方法,當然代碼會多兩行。
