如何当用户插入的Andr​​oid设备的耳机检测? (ACTION_AUDIO_BECOMING_NOISY的对面) [英] How to detect when a user plugs headset on android device? (Opposite of ACTION_AUDIO_BECOMING_NOISY)

查看:1306
本文介绍了如何当用户插入的Andr​​oid设备的耳机检测? (ACTION_AUDIO_BECOMING_NOISY的对面)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发具有以下所需的应用程序:如果插在设备中的耳机和用户删除它,我需要静音所有流。要做到这一点,我需要听 AudioManager.ACTION_AUDIO_BECOMING_NOISY 广播。这是确定的!没有问题就在这里。

I'm developing an application that has the following requisite: If there is a headset plugged in the device and the user removes it, I need to mute all streams. To do that I need to listen to the AudioManager.ACTION_AUDIO_BECOMING_NOISY Broadcast. This is ok! Not problem here.

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

But when the user plugs the headset again, I need to un-mute the device. But there isn't a AudioManager.ACTION_AUDIO_BECOMING_NOISY opposite broadcast. I cannot know when the headset is plugged again.

解决方案之一是定期查看是否 AudioManager.isWiredHeadsetOn(),但这似乎并没有是一个很好的解决方案给我。

One solution is to periodically see if AudioManager.isWiredHeadsetOn() is true but this don't appear to be a good solution to me.

有没有一种方法,当用户插入设备上的耳机来检测?

Is there a way to detect when the user plugs a headset on the device?

编辑:我试图用 Intent.ACTION_HEADSET_PLUG 这样一来,但没有奏效

Edited: I tried to use Intent.ACTION_HEADSET_PLUG this way, but it didn't work.

在我把manifest.xml的:

In the manifest.xml I put:

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

这里是的code我的 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
        }
    }
}

任何其他的解决方案来试试呢?

Any other solution to try?

推荐答案

这个怎么样的呼叫: <一href="http://developer.android.com/reference/android/content/Intent.html#ACTION_HEADSET_PLUG">http://developer.android.com/reference/android/content/Intent.html#ACTION_HEADSET_PLUG 我发现,在 Droid难以置信耳机检测

How about this call: http://developer.android.com/reference/android/content/Intent.html#ACTION_HEADSET_PLUG which I found at Droid Incredible Headphones Detection ?

更新code现在我在你的问题中看到的是不够的。的广播发生堵塞的状态发生改变时,有时当它不存在,按照<一个href="http://stackoverflow.com/questions/4092438/intent-action-headset-plug-is-received-when-activity-starts">Intent.ACTION_HEADSET_PLUG活动开始时,收到所以我会写:

The updated code I see in your question now isn't enough. That broadcast happens when the plugged state changes, and sometimes when it doesn't, according to Intent.ACTION_HEADSET_PLUG is received when activity starts so I would write:

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 pcated德$ P $,所以我从广播意图提取状态取代了它。这是可能的,有可能是多个节目的每个插入或拔出,​​或许是因为触点抖动,在连接。

The AudioManager.isWiredHeadsetOn() call which I earlier recommended turns out to be deprecated since API 14, so I replaced it with extracting the state from the broadcast intent. It's possible that there could be multiple broadcasts for each plugging or unplugging, perhaps because of contact bounce in the connector.

这篇关于如何当用户插入的Andr​​oid设备的耳机检测? (ACTION_AUDIO_BECOMING_NOISY的对面)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆