Android EventHandler—给应用添加事件處理(直播vlc)

如果您想在 Android 中定義自己的元件,您可以使用事件處理者 (EventHandler),以下是常見的處理事件。

事件 呼叫時機
onKeyDown(int, KeyEvent) 當按鍵被按下時觸發
onKeyUp(int, KeyEvent) 當按鍵被放開時觸發
onTrackballEvent(MotionEvent) 當軌跡球運動時觸發
onTouchEvent(MotionEvent) 當螢幕被碰觸時發生
onFocusChanged(boolean, int, Rect) 當元件取得焦點或失去焦點時觸發

另外還有一些較為複雜的事件,像是 Layout 排版等事件,這些事件並非 View 的函數,但是卻與視覺化事件有關的,像是下列函數。

事件 呼叫時機
Activity.dispatchTouchEvent(MotionEvent) 在觸碰事件發生前就可以取得該事件
ViewGroup.onInterceptTouchEvent(MotionEvent) 允許 ViewGroup 完鍵攔截其子元件的事件
ViewParent.requestDisallowInterceptTouchEvent(boolean) 禁止攔截 onInterceptTouchEvent(MotionEvent)

以下是vlc添加事件处理的实例:

1.先是创建EventHandler类:

import java.util.ArrayList;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
public class EventHandler {
  //public static final int MediaMetaChanged                = 0;
    //public static final int MediaSubItemAdded               = 1;
    //public static final int MediaDurationChanged            = 2;
    //public static final int MediaParsedChanged              = 3;
    //public static final int MediaFreed                      = 4;
    //public static final int MediaStateChanged               = 5;
    //public static final int MediaPlayerMediaChanged         = 0x100;
    //public static final int MediaPlayerNothingSpecial       = 0x101;
    //public static final int MediaPlayerOpening              = 0x102;
    //public static final int MediaPlayerBuffering            = 0x103;
    public static final int MediaPlayerPlaying                = 0x104;
    public static final int MediaPlayerPaused                 = 0x105;
    public static final int MediaPlayerStopped                = 0x106;
    //public static final int MediaPlayerForward              = 0x107;
    //public static final int MediaPlayerBackward             = 0x108;
    public static final int MediaPlayerEndReached             = 0x109;
    public static final int MediaPlayerEncounteredError       = 0x10a;
    //public static final int MediaPlayerTimeChanged          = 0x10b;
    public static final int MediaPlayerPositionChanged        = 0x10c;
    //public static final int MediaPlayerSeekableChanged      = 0x10d;
    //public static final int MediaPlayerPausableChanged      = 0x10e;
    //public static final int MediaPlayerTitleChanged         = 0x10f;
    //public static final int MediaPlayerSnapshotTaken        = 0x110;
    //public static final int MediaPlayerLengthChanged        = 0x111;
    public static final int MediaPlayerVout                   = 0x112;
    public static final int MediaListItemAdded                = 0x200;
    //public static final int MediaListWillAddItem            = 0x201;
    public static final int MediaListItemDeleted              = 0x202;
    //public static final int MediaListWillDeleteItem         = 0x203;
    //public static final int MediaListViewItemAdded          = 0x300;
    //public static final int MediaListViewWillAddItem        = 0x301;
    //public static final int MediaListViewItemDeleted        = 0x302;
    //public static final int MediaListViewWillDeleteItem     = 0x303;
    //public static final int MediaListPlayerPlayed           = 0x400;
    //public static final int MediaListPlayerNextItemSet      = 0x401;
    //public static final int MediaListPlayerStopped          = 0x402;
    //public static final int MediaDiscovererStarted          = 0x500;
    //public static final int MediaDiscovererEnded            = 0x501;
    //public static final int VlmMediaAdded                   = 0x600;
    //public static final int VlmMediaRemoved                 = 0x601;
    //public static final int VlmMediaChanged                 = 0x602;
    //public static final int VlmMediaInstanceStarted         = 0x603;
    //public static final int VlmMediaInstanceStopped         = 0x604;
    //public static final int VlmMediaInstanceStatusInit      = 0x605;
    //public static final int VlmMediaInstanceStatusOpening   = 0x606;
    //public static final int VlmMediaInstanceStatusPlaying   = 0x607;
    //public static final int VlmMediaInstanceStatusPause     = 0x608;
    //public static final int VlmMediaInstanceStatusEnd       = 0x609;
    //public static final int VlmMediaInstanceStatusError     = 0x60a;

    private ArrayList<Handler> mEventHandler;
    private static EventHandler mInstance;

    private EventHandler() {
        mEventHandler = new ArrayList<Handler>();
    }

    public static EventHandler getInstance() {
        if (mInstance == null) {
            mInstance = new EventHandler();
        }
        return mInstance;
    }

    public void addHandler(Handler handler) {
        if (!mEventHandler.contains(handler))
            mEventHandler.add(handler);
    }

    public void removeHandler(Handler handler) {
        mEventHandler.remove(handler);
    }

    /** This method is called by a native thread *//此方法是发送事件处理的handler。被加入到
native thread。至于怎么添加进去的还是个问题*/
 public void callback(int event, Bundle b) { b.putInt("event", event); for (int i = 0; i < mEventHandler.size(); i++) { Message msg = Message.obtain(); msg.setData(b); mEventHandler.get(i).sendMessage(msg); } }}
2.然后在主类Activity中使用
//在oncreat():
 EventHandler em = EventHandler.getInstance();
        em.addHandler(eventHandler);
创建事件处理者:
	private final Handler eventHandler = new VideoPlayerEventHandler(this);
	private class VideoPlayerEventHandler extends
			WeakHandler<VideoPlayerActivity> {
		public VideoPlayerEventHandler(VideoPlayerActivity owner) {
			super(owner);
		}
		public void handleMessage(Message msg) {
			VideoPlayerActivity activity = getOwner();
			if (activity == null)
				return;
			switch (msg.getData().getInt("event")) {
			case EventHandler.MediaPlayerPlaying:
				loadingView.setVisibility(View.GONE);
				bufferView.setVisibility(View.GONE);
				break;
			case EventHandler.MediaPlayerPaused:
				loadingView.setVisibility(View.GONE);
				bufferView.setVisibility(View.VISIBLE);
				break;
			case EventHandler.MediaPlayerStopped:				 
				break;
			case EventHandler.MediaPlayerEndReached:
				break;
			case EventHandler.MediaPlayerVout:
				break;
			case EventHandler.MediaPlayerPositionChanged:
                //don't spam the logs
                break;
            case EventHandler.MediaPlayerEncounteredError:
            	handler.sendEmptyMessageDelayed(LOADING_10SEC, 100);
//                Log.i(TAG, "MediaPlayerEncounteredError");
//                activity.encounteredError();
                break;
			default:
				Log.i(TAG, "eventHandler : " + msg.getData().getInt("event"));
				break;
			}
		}
	};

程序退出是需要清除自定义的事件处理:

	protected void onDestroy() {
		EventHandler em = EventHandler.getInstance();
		em.removeHandler(eventHandler);
		mAudioManager = null;
	}



你可能感兴趣的:(Android EventHandler—给应用添加事件處理(直播vlc))