如何在 android 服务中禁用主页和后退按钮

How to disable Home and Back Button in android service

如何在弹出对话框打开时禁用服务中的主页和后退按钮单击功能。

dialog.setOnKeyListener(new Dialog.OnKeyListener() {
    @Override
    public boolean onKey(DialogInterface dialogs, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //what should i do
        }
        if (keyCode == KeyEvent.KEYCODE_HOME) {
            //what should i do
        }
        return false;
    }
});

将可取消设置为 false

dialog.setCancelable(false);

dialog.setOnKeyListener(new Dialog.OnKeyListener() {
    @Override
    public boolean onKey(DialogInterface dialogs, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_HOME) {
            //what should i do
            return true;
        }
        return false;
    }
});

如果您的上述代码在您的服务中正常工作,您只需 return true.

dialog.setOnKeyListener(new Dialog.OnKeyListener() {
    @Override
    public boolean onKey(DialogInterface dialogs, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        if (keyCode == KeyEvent.KEYCODE_HOME) {
            return true;
        }
        return false;
    }
});

注意: returning true 将指示系统您将处理进一步的事件并且没有进一步的转发需要 event