如何在Android中以编程方式禁用所有硬件密钥? [英] How to disable all hardware keys programmatically in Android?

查看:89
本文介绍了如何在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

这篇关于如何在Android中以编程方式禁用所有硬件密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆