Android - 捕获/抑制 Home 和 EndCall 按钮事件?
如果您曾经尝试在 Android 上编写储物柜应用程序,那么您一定会遇到这个问题:
boolean mBackPressed = false;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
mBackPressed = true;
break;
case KeyEvent.KEYCODE_MENU:
if (mBackPressed)
unLock();
break;
default:
mBackPressed = false;
showMessage();
break;
}
}
return true;
}
private void showMessage() {
Toast.makeText(getBaseContext(), "Back + Menu", Toast.LENGTH_SHORT)
.show();
}
private void unLock() {
this.setResult(Activity.RESULT_OK);
this.finish();
}
似乎 onKeyDown 过滤掉了除“后退”和“菜单”之外的所有键...
嗯,这不是真的!“主页”按钮仍将为您带来主屏幕,“结束通话”按钮将运行本机 Locker 应用程序!
那里的同事也声称这是一个问题:
如何通过ENDCALL按钮收听
主屏幕后屏幕按钮出现问题
抑制 Activity 中的按键,尤其是选项菜单中的按键
权限
问题 4202:功能建议:拦截 KEYCODE_CALL的 知道有什么解决方法可以阻止这两个按钮吗?
是唯一的方法(通常) - 用 C 编写?
If you ever tried to write a locker app on Android sure you meet this problem:
boolean mBackPressed = false;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
mBackPressed = true;
break;
case KeyEvent.KEYCODE_MENU:
if (mBackPressed)
unLock();
break;
default:
mBackPressed = false;
showMessage();
break;
}
}
return true;
}
private void showMessage() {
Toast.makeText(getBaseContext(), "Back + Menu", Toast.LENGTH_SHORT)
.show();
}
private void unLock() {
this.setResult(Activity.RESULT_OK);
this.finish();
}
Seems like onKeyDown is filtering out all keys but "Back" and "Menu"...
Well, it's not true! Home button will still bring you Home screen and End Call button will run native Locker application!
Fellow's out there also claim it as a problem:
How to listen from ENDCALL button
problem With Home Back screen button
Supressing Key presses in Activity, especially in Options Menu
Issue 4202: Feature Suggestion: permission for intercepting KEYCODE_CALL
Do you know any workaround to block two those buttons?
Is the only way (as often) - write in C ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以捕获后退键很容易。
我认为您无法拦截“Home”和“End Call”按钮。如果可以的话,这将允许恶意应用程序阻止用户离开它,从而有效地劫持手机。
您的应用程序的一个选项是使用
android.intent.category.HOME
意图。You can capture the Back key quite easily.
I don't think you'll be able to intercept the Home and End Call buttons. If you could, this would allow a malicious application to prevent a user ever leaving it, effectively hijacking the phone.
An option for your application would be to write a replacement Home Screen using the
android.intent.category.HOME
Intent.