用于拔下耳机的广播接收器

Posted

技术标签:

【中文标题】用于拔下耳机的广播接收器【英文标题】:BroadcastReceiver for unplug of headphone 【发布时间】:2012-07-30 06:20:09 【问题描述】:

您好,我正在开发一个应用程序,该应用程序会在耳机从手机上取下时生成一个事件。 我创建了一个接收方法为的广播接收器

public void onReceive(Context context, Intent intent) 

        // TODO Auto-generated method stub
        String action = intent.getAction();
        Log.i("Broadcast Receiver", "Hello");
        if( (action.compareTo(Intent.ACTION_HEADSET_PLUG))  == 0)   //if the action match a headset one
        
            int headSetState = intent.getIntExtra("state", 0);      //get the headset state property
            int hasMicrophone = intent.getIntExtra("microphone", 0);//get the headset microphone property
            if( (headSetState == 0) && (hasMicrophone == 0))        //headset was unplugged & has no microphone
            

                    //do whatever
            
                   

    

如下调用该方法

 IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
        HeadSetBroadCastReceiver receiver = new HeadSetBroadCastReceiver();
        registerReceiver( receiver, receiverFilter );

我也已在清单中将此注册为

   <receiver android:name=".HeadsetBroadCastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.ACTION_HEADSET_PLUG"/>
    </intent-filter>
</receiver>

和许可

但这不起作用,谁能指导我完成这个?

【问题讨论】:

普通人没人能指导我吗? Check this one This works 我已经检查过了,但 logcatif (intent.hasExtra("state")) if (headsetConnected && intent.getIntExtra("state", 0) == 0)耳机连接 = 假; Log.d("值","内部"); else if (!headsetConnected && intent.getIntExtra("state", 0) == 1) 耳机连接 = true; 你的onReceive()被叫了吗? 【参考方案1】:

这是一个棘手的问题,但您可以使用 BroadCast 作为以下与我合作得很好 在您的活动

protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myReceiver = new HeadSetReceiver();
 

并在 onResume() 方法中注册你的广播

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

然后在您的活动中声明您的广播

private class HeadSetReceiver 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 unplugged");
                break;
            case 1:
                Log.d(TAG, "Headset plugged");
                break;
            
        
    

希望对你有帮助,,,

【讨论】:

【参考方案2】:

这里的问题是这个广播设置了标志Intent.FLAG_RECEIVER_REGISTERED_ONLY。这意味着清单接收者不会捕捉到这一点。 Here 是完整的解释。

【讨论】:

【参考方案3】:

我的问题的答案是这样的。谢谢大家的回复

class NoisyAudiostreamReceiver extends BroadcastReceiver

    @Override
    public void onReceive(Context context, Intent intent) 
        if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) 
           // pause();
            Log.d("Mesage","Unplug");
            Toast.makeText(context, "Heello",Toast.LENGTH_LONG).show();
        
    

 IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
        NoisyAudioStreamReceiver receiver = new NoisyAudioStreamReceiver();
        registerReceiver( receiver, intentFilter );

【讨论】:

【参考方案4】:

我建议您在 onReceive 中切换断点,并在调试模式下检查您在插入手机设备时收到的事件类型。 然后插入这个事件而不是 Intent.ACTION_HEADSET_PLUG 谢谢。

【讨论】:

我认为广播接收器没有注册。:(【参考方案5】:

我想用各自的意图注册广播接收器存在问题。参考这个post 看看是否可行。

我的 logcat 中的异常

07-30 12:41:01.448: E/ActivityThread(321): Activity com.example.test.MainActivity has leaked IntentReceiver com.example.test.BroadcastsHandler@44ee81d8 that was originally registered here. Are you missing a call to unregisterReceiver()?
07-30 12:41:01.448: E/ActivityThread(321): android.app.IntentReceiverLeaked: Activity com.example.test.MainActivity has leaked IntentReceiver com.example.test.BroadcastsHandler@44ee81d8 that was originally registered here. Are you missing a call to unregisterReceiver()?
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.ActivityThread$PackageInfo$ReceiverDispatcher.<init>(ActivityThread.java:939)
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.ActivityThread$PackageInfo.getReceiverDispatcher(ActivityThread.java:734)
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:791)
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.ContextImpl.registerReceiver(ContextImpl.java:778)
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.ContextImpl.registerReceiver(ContextImpl.java:772)
07-30 12:41:01.448: E/ActivityThread(321):  at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318)
07-30 12:41:01.448: E/ActivityThread(321):  at com.example.test.MainActivity.onCreate(MainActivity.java:19)
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-30 12:41:01.448: E/ActivityThread(321):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 12:41:01.448: E/ActivityThread(321):  at android.os.Looper.loop(Looper.java:123)
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-30 12:41:01.448: E/ActivityThread(321):  at java.lang.reflect.Method.invokeNative(Native Method)
07-30 12:41:01.448: E/ActivityThread(321):  at java.lang.reflect.Method.invoke(Method.java:521)
07-30 12:41:01.448: E/ActivityThread(321):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-30 12:41:01.448: E/ActivityThread(321):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-30 12:41:01.448: E/ActivityThread(321):  at dalvik.system.NativeStart.main(Native Method)

这就是我在我的一个项目中注册广播接收器的方式:

创建:

$////////////////broadcast reciever/////////////
    IntentFilter iFilter = new IntentFilter();
    iFilter.addAction(handler.ACTION);
    iFilter.addCategory(Intent.CATEGORY_DEFAULT);
    handler = new HandleTime();
    registerReceiver(handler, iFilter);

Manifest.xml:

<activity android:name=".MainActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

【讨论】:

以上是关于用于拔下耳机的广播接收器的主要内容,如果未能解决你的问题,请参考以下文章

android/或替代蓝牙耳机的粘性广播意图

Android 高性能音频Oboe 音频流打开后 耳机 / 音箱 插拔事件处理 ( 动态注册广播接收者监听耳机插拔事件 | 重新打开 Oboe 音频流 )

用于应用权限的 Android 广播接收器

用于更改日期的广播接收器

广播接收器不适用于SMS

如何将“goAsync”用于广播接收器?