BroadcastReceiver的耳机中拔出 [英] BroadcastReceiver for unplug of headphone

查看:212
本文介绍了BroadcastReceiver的耳机中拔出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我的工作产生过当耳机从手机中删除的事件的应用程序。 我创建了一个广播接收器与接收方法

Hi i am working on an application that generate an event when ever the headphone is removed from the mobile phone. I have created a broadcast receiver with receive method as

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
            }
        }           

    }

调用此方法如下:

Calling this method as follows

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

另外我有登记本中表现为

also i have register this in manifest as

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

和许可

但是,这不列入工程任何人都可以指导我通过这个?

But this doesnot works can anyone guide me through this?

推荐答案

这里的问题是,这个braodcast具有标志<一个href="http://developer.android.com/reference/android/content/Intent.html#FLAG_RECEIVER_REGISTERED_ONLY"相对=nofollow> Intent.FLAG_RECEIVER_REGISTERED_ONLY设定。这意味着清单recivers不会赶上这一点。 <一href="http://stackoverflow.com/questions/6249023/detecting-whether-a-headset-is-plugged-in-or-not/6366238#6366238">Here是一个完整的explenation。

Problem here is that this braodcast has flag Intent.FLAG_RECEIVER_REGISTERED_ONLY set. This means that manifest recivers will not catch that. Here is a full explenation.

这篇关于BroadcastReceiver的耳机中拔出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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