我正在尝试以编程方式将音量提高到STREAM_MUSIC
流的最大值,但是当我这样做时,我遇到了“向死线程上的处理程序发送消息”的问题。此外,它似乎不会在 100% 的时间里提高音量,尽管当我收到这个错误时,它会在大多数情况下提高音量。
代码是:
System.out.println("Maximum volume for this stream is: "+maxstreamvol+" and it used to be set to: "+currentvol);
final AudioManager am = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
am.setStreamVolume(AudioManager.STREAM_MUSIC, maxstreamvol, AudioManager.FLAG_SHOW_UI);
am.setStreamSolo(AudioManager.STREAM_MUSIC, true);
System.out.println("Volume Raised!");
谷歌搜索后,似乎这个错误与多线程情况有关......此时的代码应该在UI线程上运行。
事实上,我什至用以下内容包围了它:
runOnUiThread(new Runnable() {
public void run() {
// code_goes_here
}
});
而且,这产生了同样的错误。我看到的错误是这样的:
I/System.out(24949): Maximum volume for this stream is: 15 and it used to be set to: 0
W/MessageQueue( 490): Handler (android.media.AudioManager$FocusEventHandlerDelegate$1) {42b52f28} sending message to a Handler on a dead thread
W/MessageQueue( 490): java.lang.RuntimeException: Handler (android.media.AudioManager$FocusEventHandlerDelegate$1) {42b52f28} sending message to a Handler on a dead thread
W/MessageQueue( 490): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:294)
W/MessageQueue( 490): at android.os.Handler.enqueueMessage(Handler.java:618)
W/MessageQueue( 490): at android.os.Handler.sendMessageAtTime(Handler.java:587)
W/MessageQueue( 490): at android.os.Handler.sendMessageDelayed(Handler.java:558)
W/MessageQueue( 490): at android.os.Handler.sendMessage(Handler.java:495)
W/MessageQueue( 490): at android.media.AudioManager$1.dispatchAudioFocusChange(AudioManager.java:1894)
W/MessageQueue( 490): at android.media.IAudioFocusDispatcher$Stub.onTransact(IAudioFocusDispatcher.java:57)
W/MessageQueue( 490): at android.os.Binder.execTransact(Binder.java:351)
W/MessageQueue( 490): at dalvik.system.NativeStart.run(Native Method)
I/System.out(24949): Volume Raised!
有人知道这里发生了什么吗?