安卓盒子按钮后门和退出的方法

来源:互联网 发布:液压制图软件 编辑:程序博客网 时间:2024/05/16 12:08
1、按2次返回键退出程序
首先增加变量
// 定义一个变量,来标识是否退出
private static booleanisExit = false;
然后返回按钮事件
if (!webView.canGoBack()) {
exit();
return true;
} else {
webView.goBack();
return false;
}
方法(else里的js方法是自定义方法,不需要)
private void exit() {
if (!isExit) {
isExit= true;
Toast.makeText(getApplicationContext(), "再按一次退出",
Toast.LENGTH_SHORT).show();
// 利用handler延迟发送更改状态信息
mHandler.sendEmptyMessageDelayed(0, 2000);
} else {
webView.loadUrl("javascript:cleanFocusCache()");
finish();
}
}

2、后门
首先增加后门按钮顺序
private int[] surprise = new int[] { 1, KeyEvent.KEYCODE_DPAD_UP,
KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_UP,
KeyEvent.KEYCODE_DPAD_LEFT};
后门按钮方法
private void surprise(int keyCode) {
if (keyCode == surprise[surprise[0]]) {

surprise[0] = (surprise[0]) % surprise.length + 1;

if (surprise[0] == surprise.length) {

surprise[0] = 1;

Intent intent = new Intent(MainActivity.this,
SystemSettingActivity.class);
startActivityForResult(intent, 0);
}
} else {

surprise[0] = 1;
}
}

后门按钮方法调用
if (event.getAction() == KeyEvent.ACTION_DOWN) {
surprise(event.getKeyCode());
}
原创粉丝点击