如何在Android中以编程方式禁用所有硬件密钥?

问题描述:

我正在开发具有锁定功能的Android应用程序。请建议我如何以编程方式禁用所有硬键。在这里,我使用下面的代码禁用后退按钮。我想为所有硬键(例如家庭,搜索,相机,快捷键)提供此功能,这里是我的代码:

I am developing Android application with lock functionality. please suggest me how to disable all the hard keys programmatically. here I am using below code to disable back button. I want like this functionality for all hard keys like home,search,camera, shortcut keys here is my code:

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_SEARCH) {
        Log.d("KeyPress", "search");
        return true;
    }
    return false;
}


为此修改onKey方法:

Modify your onKey method to this :

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    return true;
}

当从onKey方法返回true时,表示您已经处理了密钥压自己,防止操作系统采取默认操作。
在您的代码中,您只处理搜索按钮,但是在所有情况下都返回true时,它将阻止所有按钮。

When you return true from onKey method, it means that you have handled the key press yourself and prevents the OS from taking the default action. In you code, you are only handling the search button, but when you return true for all cases, it will block all buttons.

PS,这可能不会用于软按钮。引用

P.S this might not work for soft buttons. Refer this