首先解释下这句话This Handler class should be static or leaks might occur,大致意思就是说:Handler类应该定义成静态类,否则可能导致内存泄露。
==================================================================================================================================
-
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 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应该在使用之前对其状态进行判断。