发布:2021/10/25 9:09:51作者:管理员 来源:本站 浏览次数:1731
手机开发中我们知道有如下几个函数,至于各位有没有用过,因项目需求而异。
boolean onKeyDown(int keyCode, KeyEvent event)
boolean onKeyUp(int keyCode, KeyEvent event)
boolean dispatchKeyEvent(KeyEvent event)
boolean onTouchEvent(MotionEvent ev)
boolean dispatchTouchEvent(MotionEvent ev)
当然手机中的不是我们这篇文章要说的。
Activity和View中都有这三个事件:
boolean onKeyDown(int keyCode, KeyEvent event)
boolean onKeyUp(int keyCode, KeyEvent event)
boolean dispatchKeyEvent(KeyEvent event)
下面开始说正文:
那么有人有疑惑了,那么view的onClick是那个按键触发的呢?
是遥控器中的圆盘(上下左右)的ok按键对应的keycode=DPAD_CENTER;
我们可以在Activity中通过onKeyDown获取遥控器按键对应的keyCode码,然后做对应的操作;
当然我们也可以在dispatchKeyEvent控制按键的分发,就像我们做手机app的时候一样,
事件拦截是同一个道理;
我们也可以针对view设置对于的onkeyDown,onKeyUp事件;
我写过一个RemoteControlKeyEvent,传入对于的keyCode获得对应的类型 ,
每个项目需求不同,需要稍作修改:
public class RemoteControlKeyEvent {
/**
* 直播
*/
public static final int BTV = 1181;
/**
* 点播
*/
public static final int VOD = 1182;
/**
* 回看
*/
public static final int TVOD = 1184;
/**
* VOD快进
*/
public static final int VOD_FAST_FORWARD = KeyEvent.KEYCODE_MEDIA_FAST_FORWARD;
/**
* VOD快退
*/
public static final int VOD_FAST_REWIND = KeyEvent.KEYCODE_MEDIA_REWIND;
/**
* 媒体播放暂停
*/
public static final int MEDIA_PAUSE_PLAY = KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE;
/**
* 频道+
*/
public static final int CHANNEL_UP = KeyEvent.KEYCODE_CHANNEL_UP;
/**
* 频道-
*/
public static final int CHANNEL_DOWN = KeyEvent.KEYCODE_CHANNEL_DOWN;
/**
* 在直播界面切换出Infoview菜单
*/
public static final int CHANNEL_MENU = KeyEvent.KEYCODE_MENU;
/**
* 显示直播频道页
*/
public static final int SHOW_EPGVIEW = KeyEvent.KEYCODE_DPAD_RIGHT;
private static RemoteControlKeyEvent mKeyEvent;
private ArrayMap<Integer, Integer> mBaseKeyCodeValue = new ArrayMap<>();
private ArrayMap<Integer, Integer> mVODKeyCodeValue = new ArrayMap<>();
private ArrayMap<Integer, Integer> mBTVKeyCodeValue = new ArrayMap<>();
public int getKeyCodeValue(int code) {
if (mBaseKeyCodeValue.containsKey(code)) {
return mBaseKeyCodeValue.get(code);
}
return code;
}
public int getVODKeyCodeValue(int code) {
if (mVODKeyCodeValue.containsKey(code)) {
return mVODKeyCodeValue.get(code);
}
return code;
}
public int getBTVKeyCodeValue(int code) {
if (mBTVKeyCodeValue.containsKey(code)) {
return mBTVKeyCodeValue.get(code);
}
return code;
}
public static synchronized RemoteControlKeyEvent getInstance() {
if (null == mKeyEvent) {
mKeyEvent = new RemoteControlKeyEvent();
}
return mKeyEvent;
}
private RemoteKeyEvent() {
//BTV 直播
mBaseKeyCodeValue.put(BTV, BTV);
mBaseKeyCodeValue.put(KeyEvent.KEYCODE_PROG_RED, BTV);
//TVOD 回看
mBaseKeyCodeValue.put(TVOD, TVOD);
mBaseKeyCodeValue.put(KeyEvent.KEYCODE_PROG_GREEN, TVOD);
//点播
mBaseKeyCodeValue.put(VOD, VOD);
mBaseKeyCodeValue.put(KeyEvent.KEYCODE_PROG_YELLOW, VOD);
//0-9按键
mBaseKeyCodeValue.put(KeyEvent.KEYCODE_0, 0);
mBaseKeyCodeValue.put(KeyEvent.KEYCODE_1, 1);
mBaseKeyCodeValue.put(KeyEvent.KEYCODE_2, 2);
mBaseKeyCodeValue.put(KeyEvent.KEYCODE_3, 3);
mBaseKeyCodeValue.put(KeyEvent.KEYCODE_4, 4);
mBaseKeyCodeValue.put(KeyEvent.KEYCODE_5, 5);
mBaseKeyCodeValue.put(KeyEvent.KEYCODE_6, 6);
mBaseKeyCodeValue.put(KeyEvent.KEYCODE_7, 7);
mBaseKeyCodeValue.put(KeyEvent.KEYCODE_8, 8);
mBaseKeyCodeValue.put(KeyEvent.KEYCODE_9, 9);
//VOD快进,快退
mVODKeyCodeValue.put(KeyEvent.KEYCODE_DPAD_RIGHT, VOD_FAST_FORWARD);
mVODKeyCodeValue.put(KeyEvent.KEYCODE_PAGE_DOWN, VOD_FAST_FORWARD);
mVODKeyCodeValue.put(KeyEvent.KEYCODE_DPAD_LEFT, VOD_FAST_REWIND);
mVODKeyCodeValue.put(KeyEvent.KEYCODE_PAGE_UP, VOD_FAST_REWIND);
//VOD媒体播放暂停code
mVODKeyCodeValue.put(KeyEvent.KEYCODE_DPAD_CENTER, MEDIA_PAUSE_PLAY);
mVODKeyCodeValue.put(KeyEvent.KEYCODE_ENTER, MEDIA_PAUSE_PLAY);
mVODKeyCodeValue.put(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, MEDIA_PAUSE_PLAY);
//频道+
mBTVKeyCodeValue.put(KeyEvent.KEYCODE_CHANNEL_UP, CHANNEL_UP);
mBTVKeyCodeValue.put(KeyEvent.KEYCODE_DPAD_UP, CHANNEL_UP);
//频道-
mBTVKeyCodeValue.put(KeyEvent.KEYCODE_CHANNEL_DOWN, CHANNEL_DOWN);
mBTVKeyCodeValue.put(KeyEvent.KEYCODE_DPAD_DOWN, CHANNEL_DOWN);
//channel menu,项目需求,直播页点击DPAD_LEFT可以呼出菜单键
mBTVKeyCodeValue.put(KeyEvent.KEYCODE_DPAD_LEFT, CHANNEL_MENU);
mBTVKeyCodeValue.put(KeyEvent.KEYCODE_MENU, CHANNEL_MENU);
//直播频道页
mBTVKeyCodeValue.put(KeyEvent.KEYCODE_DPAD_RIGHT, SHOW_EPGVIEW);
}
}
当然上面的代码因需求而异,在项目中针对自己项目进行测试过之后,在引入上面的类;
下面介绍遥控器:
遥控器不一样,遥控器可以学习嘛,有的机顶盒遥控器按键功能比较多,有的机顶盒遥控器按键很基础,和海外机顶盒遥控器差不多,现在好多机顶盒的遥控器都差不多了,小米盒子3c遥控器长的如下:
电信机顶盒遥控器
遥控器大概有上面两种分格的机顶盒遥控器;
在代码编写时,根据自身需求进行定制处理。
请各位等待第三篇....................
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4