标签:ado lin end als settings ret tom filter ast
原文:Android TV开发中所有的遥控器按键监听及注意事项,新增home键监听
public class MainActivity extends Activity {
private String TAG="key";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER: //确定键enter
case KeyEvent.KEYCODE_DPAD_CENTER:
Log.d(TAG,"enter--->");
break;
case KeyEvent.KEYCODE_BACK: //返回键
Log.d(TAG,"back--->");
return true; //这里由于break会退出,所以我们自己要处理掉 不返回上一层
case KeyEvent.KEYCODE_SETTINGS: //设置键
Log.d(TAG,"setting--->");
break;
case KeyEvent.KEYCODE_DPAD_DOWN: //向下键
/* 实际开发中有时候会触发两次,所以要判断一下按下时触发 ,松开按键时不触发
* exp:KeyEvent.ACTION_UP
*/
if (event.getAction() == KeyEvent.ACTION_DOWN){
Log.d(TAG,"down--->");
}
break;
case KeyEvent.KEYCODE_DPAD_UP: //向上键
Log.d(TAG,"up--->");
break;
case KeyEvent.KEYCODE_0: //数字键0
Log.d(TAG,"0--->");
break;
case KeyEvent.KEYCODE_DPAD_LEFT: //向左键
Log.d(TAG,"left--->");
break;
case KeyEvent.KEYCODE_DPAD_RIGHT: //向右键
Log.d(TAG,"right--->");
break;
case KeyEvent.KEYCODE_INFO: //info键
Log.d(TAG,"info--->");
break;
case KeyEvent.KEYCODE_PAGE_DOWN: //向上翻页键
case KeyEvent.KEYCODE_MEDIA_NEXT:
Log.d(TAG,"page down--->");
break;
case KeyEvent.KEYCODE_PAGE_UP: //向下翻页键
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
Log.d(TAG,"page up--->");
break;
case KeyEvent.KEYCODE_VOLUME_UP: //调大声音键
Log.d(TAG,"voice up--->");
break;
case KeyEvent.KEYCODE_VOLUME_DOWN: //降低声音键
Log.d(TAG,"voice down--->");
break;
case KeyEvent.KEYCODE_VOLUME_MUTE: //禁用声音
Log.d(TAG,"voice mute--->");
break;
break;
default:
break;
}
return super.onKeyDown(keyCode, event);
}
}
home键的监听要监听广播:
class HomeRecaiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)){
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
if(SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)){
Toast.makeText(MainActivity.this,"home键触发",0).show();
Log.d(TAG, "home键触发");
}
}
}
}
在oncreate()注册广播
public final String SYSTEM_DIALOG_REASON_KEY = "reason";
public final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
/**
* 注册广播
*/
private void initReceiver() {
homeReceiver = new HomeRecaiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
registerReceiver(homeReceiver, filter);
}
退出注销广播
@Override
protected void onDestroy() {
super.onDestroy();
if(homeReceiver!=null){
unregisterReceiver(homeReceiver);
}
}
Android TV开发中所有的遥控器按键监听及注意事项,新增home键监听
标签:ado lin end als settings ret tom filter ast
原文地址:https://www.cnblogs.com/lonelyxmas/p/9855705.html