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?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
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:
Then, register your broadcast receiver either dynamically or statically. I registered mine dynamically in my Activity's onCreate() method:
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:
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 theandroid.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.