【问题标题】:How to detect when a user plugs headset on android device? (Opposite of ACTION_AUDIO_BECOMING_NOISY)如何检测用户何时在 android 设备上插入耳机? (与 ACTION_AUDIO_BECOMING_NOISY 相反)
【发布时间】:2012-11-16 14:38:44
【问题描述】:

我正在开发一个具有以下必要条件的应用程序:如果设备中插入了耳机并且用户将其移除,我需要将所有流静音。为此,我需要收听AudioManager.ACTION_AUDIO_BECOMING_NOISY 广播。还行吧!这里没问题。

但是当用户再次插入耳机时,我需要取消设备静音。但是没有AudioManager.ACTION_AUDIO_BECOMING_NOISY对面广播。我不知道耳机何时重新插入。

一种解决方案是定期查看AudioManager.isWiredHeadsetOn() 是否为true,但这对我来说似乎不是一个好的解决方案。

有没有办法检测用户何时将耳机插入设备?

已编辑:我尝试以这种方式使用Intent.ACTION_HEADSET_PLUG但它不起作用

在 manifest.xml 我放了:

<receiver android:name=".MusicIntentReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.HEADSET_PLUG" />
    </intent-filter>
</receiver>

这是我MusicIntentReceiver.java的代码:

public class MusicIntentReceiver extends BroadcastReceiver {

    public void onReceive(Context ctx, Intent intent) {
        AudioManager audioManager = (AudioManager)ctx.getSystemService(Context.AUDIO_SERVICE);
        if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
            Log.d("Let's turn the sound on!");
            //other things to un-mute the streams
        }
    }
}

还有其他解决方案可以尝试吗?

【问题讨论】:

标签: android broadcastreceiver android-audiomanager


【解决方案1】:

这个电话怎么样: http://developer.android.com/reference/android/content/Intent.html#ACTION_HEADSET_PLUG 我在 Droid Incredible Headphones Detection ?

我现在在您的问题中看到的更新代码还不够。根据Intent.ACTION_HEADSET_PLUG is received when activity starts,当插入状态发生变化时,有时会发生广播,所以我会写:

package com.example.testmbr;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

public class MainActivity extends Activity  {
private static final String TAG = "MainActivity";
private MusicIntentReceiver myReceiver;

@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myReceiver = new MusicIntentReceiver();
}

@Override public void onResume() {
    IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    registerReceiver(myReceiver, filter);
    super.onResume();
}

private class MusicIntentReceiver extends BroadcastReceiver {
    @Override public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
            int state = intent.getIntExtra("state", -1);
            switch (state) {
            case 0:
                Log.d(TAG, "Headset is unplugged");
                break;
            case 1:
                Log.d(TAG, "Headset is plugged");
                break;
            default:
                Log.d(TAG, "I have no idea what the headset state is");
            }
        }
    }
}

@Override public void onPause() {
    unregisterReceiver(myReceiver);
    super.onPause();
}
}

我之前推荐的 AudioManager.isWiredHeadsetOn() 调用自 API 14 以来已被弃用,因此我将其替换为从广播意图中提取状态。每次插拔可能会有多个广播,可能是因为连接器中的接触反弹。

【讨论】:

  • 你注册它来接收广播了吗? IntentFilter filter=new IntentFilter(AudioManager.ACTION_HEADSET_PLUG); registerReceiver(this,filter); 在 MusicIntentReceiver 的构造函数中。
  • 其实这个Broadcast只有我们注册一个Receiver才能收到。它是FLAG_RECEIVER_REGISTERED_ONLY。我们需要在第一个活动或服务中注册一个接收器。你能用这些信息更新你的答案吗?
  • Renato,该代码对您有用吗?我在手机上试过了,似乎对我有用。
  • 对于正在阅读本文的任何人,不要忘记 isInitialStickyBroadcast() 否则你会遇到一些麻烦 :) stackoverflow.com/a/4452190/1341947
  • AudioManager.isWiredheadSetOn 已弃用,但仍然有效。 developer.android.com/reference/android/media/… 的文档特别建议使用它来确定耳机是否已插入。
【解决方案2】:

我没有处理过这个问题,但如果我正确阅读了文档,ACTION_AUDIO_BECOMING_NOISY 是为了让应用知道音频输入可能会开始听到音频输出。当您拔下耳机时,手机的麦克风可能会开始拾取手机的扬声器,因此会收到消息。

另一方面,ACTION_SCO_AUDIO_STATE_UPDATED 旨在让您知道蓝牙设备的连接状态何时发生变化。

第二个可能是你想听的。

【讨论】:

  • 此操作仅适用于蓝牙耳机吗?您认为它适用于插孔式耳机吗?
  • 我认为这仅适用于正在连接/断开连接的蓝牙设备。对于音频插孔插头,请尝试监听 ACTION_HEADSET_PLUG 意图。
  • 不,它也适用于插口式耳机。
猜你喜欢
  • 2011-09-09
  • 2010-10-26
  • 1970-01-01
  • 2012-05-27
  • 2020-08-17
  • 2019-06-24
  • 1970-01-01
  • 2013-05-08
  • 1970-01-01
相关资源
最近更新 更多