本文介绍了活动开始时,Intent.ACTION_HEADSET_PLUG收到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想暂停音乐播放,当耳机被拔掉。

我已经创建了一个BroadcastReceiver监听ACTION_HEADSET_PLUG意图和作用在他们当的状态的额外为0(对于不插电)。我的问题是,每当所述活动开始接收通过我的BroadcastReceiver的一个ACTION_HEADSET_PLUG意图。这不是我所期望的行为。我期望的意图被解雇,只有当耳机插入或拔出。

还有一个原因是,ACTION_HEADSET_PLUG意图是注册一个接收器与IntentFilter的后立即引起了?有没有一种明确的方式,我可以就此问题开展工作?

我会假设,因为默认的音乐播放器,实现了类似的功能,当耳机被拔掉,这将是可能的。

我在想什么?

这是注册code

  registerReceiver(新HeadsetConnectionReceiver()
                 新的IntentFilter(Intent.ACTION_HEADSET_PLUG));
 

这是HeadsetConnectionReceiver的定义

 公共类HeadsetConnectionReceiver扩展的BroadcastReceiver {

    公共无效的onReceive(上下文的背景下,意图意图){
        Log.w(TAG,ACTION_HEADSET_PLUG意图获得);
    }

}
 

解决方案

感谢您的答复杰克。我已经更新了原来的职位,以表明我发现,我是有这个问题。经过一番研究,我发现ACTION_HEADSET_PLUG目的是利用广播 sendStickyBroadcast 方法融会贯通。

置顶意图由系统正在播出后举行。每当有新的BroadcastReceiver被注册为接收它那意图会被抓住。它包含上次更新的值在注册后立即触发。在耳机的情况下,这是非常有用的能够确定该耳机已经插上,当你第一次注册接收器

这是在code,我曾经收到ACTION_HEADSET_PLUG意图:

 私人布尔headsetConnected = FALSE;

 公共无效的onReceive(上下文的背景下,意图意图){
     如果(intent.hasExtra(国家)){
         如果(headsetConnected&安培;&安培; intent.getIntExtra(国家,0)== 0){
             headsetConnected = FALSE;
             如果(IsPlaying模块()){
                stopStreaming();
             }
         }否则如果(headsetConnected&安培;!&安培; intent.getIntExtra(国家,0)== 1){
            headsetConnected = TRUE;
         }
     }
 }
 

I am trying to pause music that is playing when the headset is unplugged.

I have created a BroadcastReceiver that listens for ACTION_HEADSET_PLUG intents and acts upon them when the state extra is 0 (for unplugged). My problem is that an ACTION_HEADSET_PLUG intent is received by my BroadcastReceiver whenever the activity is started. This is not the behavior that I would expect. I would expect the Intent to be fired only when the headset is plugged in or unplugged.

Is there a reason that the ACTION_HEADSET_PLUG Intent is caught immediately after registering a receiver with that IntentFilter? Is there a clear way that I can work with this issue?

I would assume that since the default music player implements similar functionality when the headset is unplugged that it would be possible.

What am I missing?

This is the registration code

registerReceiver(new HeadsetConnectionReceiver(),
                 new IntentFilter(Intent.ACTION_HEADSET_PLUG));

This is the definition of HeadsetConnectionReceiver

public class HeadsetConnectionReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        Log.w(TAG, "ACTION_HEADSET_PLUG Intent received");
    }

}
解决方案

Thanks for the reply Jake. I should have updated the original post to indicate that I discovered the issue that I was having. After a bit of research, I discovered that the ACTION_HEADSET_PLUG Intent is broadcast using the sendStickyBroadcast method in Context.

Sticky Intents are held by the system after being broadcast. That Intent will be caught whenever a new BroadcastReceiver is registered to receive it. It is triggered immediately after registration containing the last updated value. In the case of the headset, this is useful to be able to determine that the headset is already plugged in when you first register your receiver.

This is the code that I used to receive the ACTION_HEADSET_PLUG Intent:

 private boolean headsetConnected = false;

 public void onReceive(Context context, Intent intent) {
     if (intent.hasExtra("state")){
         if (headsetConnected && intent.getIntExtra("state", 0) == 0){
             headsetConnected = false;
             if (isPlaying()){
                stopStreaming();
             }
         } else if (!headsetConnected && intent.getIntExtra("state", 0) == 1){
            headsetConnected = true;
         }
     }
 }

这篇关于活动开始时,Intent.ACTION_HEADSET_PLUG收到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 02:03