gpt4 book ai didi

android - Intent.ACTION_HEADSET_PLUG 的状态 extra 总是返回 1

转载 作者:行者123 更新时间:2023-11-30 02:48:38 25 4
gpt4 key购买 nike

我正在使用 BroadcastReceiver 来了解耳机是否已插入设备,但在任何情况下“状态” Intent 都会立即返回 1(已插入)。

这是我的代码:

public class Class {

boolean isHeadsetOn;
IntentFilter headsetReceiverFilter;
HeadsetPluggedReceiver headsetReceiver;

public Class(Context context) {
this.headsetReceiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
this.headsetReceiver = new HeadsetPluggedReceiver();
context.registerReceiver(this.headsetReceiver, this.headsetReceiverFilter);
}

private class HeadsetPluggedReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
switch(intent.getIntExtra("state", -1)) {
case 0:
Synthesizer.this.isHeadsetOn = false;
Log.d("onreceive", "off");
case 1:
Synthesizer.this.isHeadsetOn = true;
Log.d("onreceive", "on");
default:
// shit
}
}

}

}

这是我的日志:

// not plugged. plugging in
onreceive | on
// unplugging
onreceive | off
onreceive | on
// plugging in again
onreceive | on
// plugging off again
onreceive | off
onreceive | on

我该如何解决这个问题?

提前谢谢你。

最佳答案

您需要在 switch case 中使用 break 语句。现在您的逻辑正在落入案例 1。

关于android - Intent.ACTION_HEADSET_PLUG 的状态 extra 总是返回 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24596930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com