概述
需求场景:在AndroidTv上隐藏式调出不开放给用户的开发模式或者工厂模式。
思路:在设置或者特定的页面下,按指定顺序按下遥控按键触发。
封装类如下:
import android.util.Log;
import android.view.KeyEvent;
/***
* LankyBin 2022.08.19
* 隐藏入口
* 组合顺序按键调用隐藏动作
*/
public class HiddenEntrance {
private final String TAG = this.getClass().getSimpleName();
private final int[] mPassword;
public HiddenEntrance(int[] password) {
mPassword = password;
}
private OnPasswordCheckedListener mListener = new OnPasswordCheckedListener() {
@Override
public void onPasswordChecked(boolean passed) {
Log.d(TAG, "onPasswordChecked: " + passed + " TO DO?");
}
};
private int mPwdNo = 0;
public boolean checkPassword(int keyCode) {
boolean ret = false;
if (keyCode == KeyEvent.KEYCODE_UNKNOWN) {
return false;
}
if (keyCode == mPassword[mPwdNo]) {
++mPwdNo;
} else {
mPwdNo = 0;
}
if (mPwdNo == mPassword.length) {
ret = true;
mPwdNo = 0;
}
mListener.onPasswordChecked(ret);
return ret;
}
public void setOnPasswordCheckedListener(OnPasswordCheckedListener listener) {
mListener = listener;
}
public interface OnPasswordCheckedListener {
void onPasswordChecked(boolean passed);
}
}
调用方式demo:
private static final int[] PASSWORD = {
KeyEvent.KEYCODE_DPAD_UP,
KeyEvent.KEYCODE_DPAD_LEFT,
KeyEvent.KEYCODE_MENU,
KeyEvent.KEYCODE_DPAD_DOWN,
KeyEvent.KEYCODE_DPAD_RIGHT
};
private final HiddenEntrance mHiddenEntrance = new HiddenEntrance(PASSWORD);
@Override
protected void onResume() {
mHiddenEntrance.setOnPasswordCheckedListener(new HiddenEntrance.OnPasswordCheckedListener() {
@Override
public void onPasswordChecked(boolean passed) {
if (passed) {
Log.d(TAG, "onPasswordChecked: todo");
//做你想做的事
}
}
});
super.onResume();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
mHiddenEntrance.checkPassword(keyCode);
}
return super.onKeyDown(keyCode, event);
}
最后
以上就是尊敬网络为你收集整理的安卓按键顺序检测触发隐藏入口的实现方式的全部内容,希望文章能够帮你解决安卓按键顺序检测触发隐藏入口的实现方式所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复