android - 覆盖的音量按钮影响了后退按钮?
使用下面的代码,我已经停止使用音量按钮,除非我正在流式传输音频(否则它会烦人地改变铃声音量),但“后退”按钮不起作用。
按“返回”应该进入我的手机桌面(或退出我的应用程序,就像您期望的那样),但它没有执行任何操作。如果我打开菜单,“返回”将按其应有的方式关闭菜单,但我无法离开该应用程序。
我已将代码复制到应用程序中的其他活动中,如果我在应用程序中打开另一个活动,因为“后退”按钮不起作用,我无法返回主屏幕:)
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
还没有测试过,但我认为你需要在调用 super.onKeyDown 的地方包含一个 else ,即:
否则,你将捕获所有键码并在检查音量代码后返回 false 。
Haven't tested, but I think you need to include an else where you call super.onKeyDown, ie:
Otherwise, you're capturing all keycodes and returning false after checking the volume codes.
让音量键始终控制媒体音量的一种更简单、更可靠的方法是将此行插入到 Activity 的
onCreate()
中: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()
:伙计,只需将该活动的音频上下文更改为媒体音量即可:
http://developer .android.com/reference/android/media/AudioManager.html
编辑:
在onCreate内部:
覆盖onKeyDown:
Dude, just change the audio context on that activity to media volume:
http://developer.android.com/reference/android/media/AudioManager.html
EDIT:
Inside onCreate:
Override onKeyDown: