问题描述
我正在使用清单文件来创建广播接收器,对于ACTION_HEADSET_PLUG. 但是当耳机连接/断开连接时,我无法获得广播, 我应该在清单文件中使用哪个permission以便能够接收ACTION_HEADSET_PLUG广播意图?
推荐答案
使用API 8,我收到了我的广播接收器,而不会创建服务或请求额外的权限.
您可以在主活动中定义一个类似于我在下面定义的主活动中的内部类:
public class HeadSetBroadCastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); Log.i("Broadcast Receiver", action); 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 } } } }
然后,在动态或静态地注册广播接收器.我在我的活动的oncreate()方法中动态登记我:
this.registerReceiver(headsetReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
确保使用上下文的未注册的roveisterReceiver取消注册BroadcastReceiver.在我的情况下,我在ondestroy()方法中这样做了.这应该做到这一点.
其他推荐答案
这不是一个权限,它实际上是你如何注册接收者的问题.耳机插头动作广播只能通过这样的主动注册的接收器接收:
registerReceiver(receiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
这意味着您需要拥有一个服务器保存的服务,该服务能够对接收器的引用进行引用,并且在服务终止时也是不可识别的.最后,注册接收器的服务也需要在启动时启动;与其他接收器一起拦截android.intent.action.BOOT_COMPLETED意图.对于此部分,您需要使用android.permission.RECEIVE_BOOT_COMPLETED权限.
为执行此内容的服务的完整示例,您可以查看应用程序我写的那样,这只是那个
问题描述
I am using the manifest file for creating broadcast receiver, for ACTION_HEADSET_PLUG. But I can't get the broadcast when headset is connect/disconnect, Which permission should I use inside the manifest file in order to be able to receive ACTION_HEADSET_PLUG broadcast intent?
推荐答案
With API 8, I got my broadcast receiver called without creating a service or requesting for extra permissions.
You can define an inner class within your main activity similar to the one I've defined below:
public class HeadSetBroadCastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); Log.i("Broadcast Receiver", action); 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 } } } }
Then, register your broadcast receiver either dynamically or statically. I registered mine dynamically in my Activity's onCreate() method:
this.registerReceiver(headsetReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
Make sure that you unregister your BroadcastReceiver with the Context's unregisterReceiver. In my case, I did this in the onDestroy() method. That should do it.
其他推荐答案
It's not a permission thing, it's actually a problem with how you registered the receiver. The headset plug action broadcast can only be received by actively registered receivers like this:
registerReceiver(receiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
This means that you need to have a service kept alive that holds a reference to the receiver and also unregisters it when the service is killed. Lastly, the service that registers the receiver needs to also be started on boot; which you do with yet another receiver that intercepts the android.intent.action.BOOT_COMPLETED intent. For this part, you will need to use the android.permission.RECEIVE_BOOT_COMPLETED permission.
For a full example of a service that does this stuff, you can check out an app I wrote that does just that.