本文介绍了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( (actionpareTo(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具有标志 Intent.FLAG_RECEIVER_REGISTERED_ONLY设定。这意味着清单recivers不会赶上这一点。 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的耳机中拔出
发布评论