【问题标题】:How to disable headset detection in android programmatically?如何以编程方式在android中禁用耳机检测?
【发布时间】:2014-01-08 09:17:37
【问题描述】:

我正在开发一个音频路由应用程序,它可以在耳机和/或手机扬声器之间引导音频。有什么方法可以以编程方式 停止耳机检测

编辑:我使用的是有线耳机。

【问题讨论】:

  • 是有线还是蓝牙耳机?
  • 我使用的是有线耳机。

标签: android android-intent soundpool android-audiomanager headset


【解决方案1】:

当您说“手机”时,您的意思是“有线耳机”吗?如果是这样,则有一个意图是检测一个是被插入还是被拔出:ACTION_HEADSET_PLUG。

要检查状态,您可以使用AudioManager.isWiredHeadsetOn(),但如果还有蓝牙耳机,这可能会返回 false,而是将音频路由到该耳机。

像这样:

final AudioManager audio =(AudioManager)getApplicationContext().getSystemService(AUDIO_SERVICE);
if(audio.isWiredHeadsetOn())
            {
                audio.setWiredHeadsetOn(false);
                audio.setSpeakerphoneOn(true);


            }

但在那之前

所以,你可以把这段代码写成“AndroidManifest.xml

<receiver android:name="com.juno.brheadset.HeadsetStateReceiver">
    <intent-filter>
        <action android:name="android.intent.action.HEADSET_PLUG"/>
    </intent-filter>
</receiver>-->

但是,这不起作用。当操作系统发送此“HEADSET_PLUG”意图时,操作系统设置标志“Intent.FLAG_RECEIVER_REGISTERED_ONLY”因此,您应该在 Activity 或 Service 类中编写如下代码,而不是“AndroidManifest”。

public class BRHeadsetActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    HeadsetStateReceiver receiver = new HeadsetStateReceiver();
    registerReceiver( receiver, receiverFilter );


}

这样你就可以解决这个问题了。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-17
  • 1970-01-01
  • 2018-05-16
  • 2011-02-16
  • 1970-01-01
  • 2010-12-15
  • 2011-01-13
相关资源
最近更新 更多