如何覆盖我的Andr​​oid应用程序之外的侧面的音量键我的、音量、侧面、应用程序

2023-09-12 22:22:03 作者:弑杀天下

我想禁用侧面的音量按钮,以便控制音量的唯一途径将是在我的Andr​​oid应用程序专用的活动。 我设法禁用它为我所有的活动中加入以下code:

  @覆盖
公共布尔的onkeydown(INT键code,KeyEvent的事件){
    Log.d(TAG的onkeydown =+键code);
    如果(键code == KeyEvent.KEY code_VOLUME_DOWN){
        返回true;
        }
    如果(键code == KeyEvent.KEY code_VOLUME_UP){
        返回true;
    }
    返回super.onKeyDown(键code,事件);
 

}

但我不知道如何禁用它,因为我从我的应用程序启动的活动(例如,我开始在画廊的应用程序)!

我知道这是可能的卷弹部队的应用程序做类似的东西。

解决方案

这是不可能在公共API来燮preSS的关键事件的自己的活动之外,如果有一个应用程序,已经成功地做​​到这一点他们在做什么会被认为是恶意的平台设计,并得到固定在某一点。

:根据所给出的该应用程序(我从来没有使用过个人注)的说明

  

prevent意外更改您的音量设置,安装卷储物柜今天。   这个程序可以帮助防止意外的体积变化prevent通过确认您所做的更改,方法是托盘通知或弹出。如果您不同意的变化,体积会在几秒钟内固定金额...复位通过设置超时为即时,锁定的卷将立即恢复,而不会提示。

我怀疑什么,实际上是做的是监听的这个答案使用类似的技术,以一个音量按钮,只是恢复任何改变是瞬间(ISH)。这将使它看起来给用户类似的关键preSS什么也没做,但在现实中发生的事情是体积变化,然后迅速变回来。

I would like to disable the side volume buttons so the only way to control the volume will be from a dedicated activity inside my android app. I managed to disable it for all my activities by adding the following code:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    Log.d(TAG, "onKeyDown = " + keyCode);
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        return true;
        }
    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
        return true;
    }
    return super.onKeyDown(keyCode, event);

}

But I don't know how to disable it for the activities i start from my app (for example I start the gallery app)!

I know it is possible as 'Volume Locker' app doing similar stuff.

解决方案

It is not possible within public APIs to suppress the key events outside of your own Activities, if there is an app that has managed to do it what they are doing would be considered malicious by the platform designers and will get fixed at some point.

Based on the description given for that app (note: I've never used it personally)

Prevent accidental changes to your volume settings, install Volume Locker today. This app helps prevent against accidental volume changes by confirming the change you made, by either tray notification or a pop up. If you don't approve the change, the volume will be reset within a set amount of seconds... By setting the timeout to "instant", the locked volumes will revert instantly without prompting.

I suspect what that is actually doing is listening for the volume buttons using a similar technique to the one in this answer and just reverting whatever change was made instantly(ish). That would make it seem to the user like the key press did nothing but in reality what happened is the volume changed and then quickly changed back.