码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-09-18 19:25:28      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

问题描述:

监听系统广播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

标签:

原文地址:http://www.cnblogs.com/luoqiuyu/p/5882580.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!