首页 > 代码库 > 监测耳机拔除广播延迟ACTION_HEADSET_PLUG broadcast delay

监测耳机拔除广播延迟ACTION_HEADSET_PLUG broadcast delay

问题描述:

监听系统广播ACTION_HEADSET_PLUG,耳机拔出时暂停播放,然而实际处理时会有延迟,耳机拔出并没有立即暂停播放,而是延迟几秒之后才暂停

解决办法:

监听AudioManager.ACTION_AUDIO_BECOMING_NOISY,解决了这个延迟的问题

public class EarPhoneReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {//        if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)){          if (intent.getAction().equals(AudioManager.ACTION_AUDIO_BECOMING_NOISY)){                   //处理事件          }    }}   private void registerEarPhoneReceiver(){        earPhoneReceiver = new EarPhoneReceiver();        IntentFilter filter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY); //       filter.addAction("android.intent.action.HEADSET_PLUG");        registerReceiver(earPhoneReceiver,filter);    }

弊端:监测不到耳机插入事件

参考博客:http://stackoverflow.com/questions/4124368/action-headset-plug-broadcast-delay/4267213#4267213

http://blog.csdn.net/mu399/article/details/38516039

监测耳机拔除广播延迟ACTION_HEADSET_PLUG broadcast delay