android - 覆盖的音量按钮影响了后退按钮?

发布于 2024-10-27 08:22:41 字数 792 浏览 7 评论 0原文

使用下面的代码,我已经停止使用音量按钮,除非我正在流式传输音频(否则它会烦人地改变铃声音量),但“后退”按钮不起作用。

按“返回”应该进入我的手机桌面(或退出我的应用程序,就像您期望的那样),但它没有执行任何操作。如果我打开菜单,“返回”将按其应有的方式关闭菜单,但我无法离开该应用程序。

我已将代码复制到应用程序中的其他活动中,如果我在应用程序中打开另一个活动,因为“后退”按钮不起作用,我无法返回主屏幕:)

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    //Suppress the use of the volume keys unless we are currently listening to the stream
    if(keyCode==KeyEvent.KEYCODE_VOLUME_UP) {
        if(StreamService.INT_PLAY_STATE==0){
            return true;
        }else{
            return false;
        }
    }
    if(keyCode==KeyEvent.KEYCODE_VOLUME_DOWN) {
        if(StreamService.INT_PLAY_STATE==0){
            return true;
        }else{
            return false;
        }
    }
return false;

为什么会发生这种情况?

Using the code below I have stopped the use of the volume buttons unless I am streaming audio (otherwise it annoyingly changes the ringer volume), but the 'Back' button isn't working.

Pressing 'back' should got to my phones desktop (or exit my app, like you would expect), but it isn't doing anything. If I open the menu, 'Back' will close the menu as it should, but I can't leave the app.

I have copied the code onto other activities within my app, if I open another activity within my app, because the 'Back' button isn't working, I can't go back to the main screen :)

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    //Suppress the use of the volume keys unless we are currently listening to the stream
    if(keyCode==KeyEvent.KEYCODE_VOLUME_UP) {
        if(StreamService.INT_PLAY_STATE==0){
            return true;
        }else{
            return false;
        }
    }
    if(keyCode==KeyEvent.KEYCODE_VOLUME_DOWN) {
        if(StreamService.INT_PLAY_STATE==0){
            return true;
        }else{
            return false;
        }
    }
return false;

Why is this happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初吻给了烟 2024-11-03 08:22:41

还没有测试过,但我认为你需要在调用 super.onKeyDown 的地方包含一个 else ,即:

if(keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
   code
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
   more code
} else {
   super.onKeyDown(keyCode, event);
}

否则,你将捕获所有键码并在检查音量代码后返回 false 。

Haven't tested, but I think you need to include an else where you call super.onKeyDown, ie:

if(keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
   code
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
   more code
} else {
   super.onKeyDown(keyCode, event);
}

Otherwise, you're capturing all keycodes and returning false after checking the volume codes.

少女情怀诗 2024-11-03 08:22:41

让音量键始终控制媒体音量的一种更简单、更可靠的方法是将此行插入到 Activity 的 onCreate() 中:

setVolumeControlStream(AudioManager.STREAM_MUSIC);

A simpler and more robust way to have the volume keys always control the Media volume is to insert this line into your Activity's onCreate():

setVolumeControlStream(AudioManager.STREAM_MUSIC);
醉梦枕江山 2024-11-03 08:22:41

伙计,只需将该活动的音频上下文更改为媒体音量即可:

http://developer .android.com/reference/android/media/AudioManager.html

编辑:

private AudioManager audio;

在onCreate内部:

audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

覆盖onKeyDown:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
        audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
        return true;
    default:
        return false;
    }
}

Dude, just change the audio context on that activity to media volume:

http://developer.android.com/reference/android/media/AudioManager.html

EDIT:

private AudioManager audio;

Inside onCreate:

audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

Override onKeyDown:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
        audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
        return true;
    default:
        return false;
    }
}
初吻给了烟 2024-11-03 08:22:41
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
        //your code
        return true;

    } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        //your code
        return true;
    } else {

        super.onKeyDown(keyCode, event);
    }


    return true;

}
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
        //your code
        return true;

    } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        //your code
        return true;
    } else {

        super.onKeyDown(keyCode, event);
    }


    return true;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文