android插拔耳麦广播android.intent.action.HEADSET_PLUG中间出的问题
2011-09-14 20:03
155 查看
今天在做插拔耳麦广播遇到一个奇怪的现象。
本来想把广播做成全局的,在Manifest文件中配置
<receiver android:name=".receiver.PlayMusicReceiver" android:enabled="true" > <intent-filter> <action android:name="android.intent.action.HEADSET_PLUG"/> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver>
结果不给力,插拔耳麦时广播没起作用。
木办法,将广播接收器写到某个Activity里面再试,奇了怪了,还起作用了,为什么设成全局的就不起作用呢?
网上搜了一下,下面给出了答案:
However, you need to be aware that HEADSET_PLUG is a "sticky" event, every BroadcastReceiver subscribed to that event will receive it upon construction; I don't know if there is a possibility to determine if the
event has captured at the exact time or been "sticked" for a while.
http://groups.google.com/group/android-developers/browse_thread/thread/6d0dda99b4f42c8f
同时也给出了解决方案,将广播注册到Service中:
public class PlayMusicService extends Service { private final PlayMusicReceiver receiver = new PlayMusicReceiver(); private static final String TAG = "PlayMusicService"; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { registerReceiver(receiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG)); super.onCreate(); Log.i(TAG, "Create Service"); } @Override public void onDestroy() { unregisterReceiver(receiver); super.onDestroy(); Log.i(TAG, "Destroy Service"); } }在Activity中用startService启动就可以了,这样在退出时仍然可以监测到耳麦的插拔。但问题又出来,如果没启动应用,则监听不到广播,达不到在manifest注册的效果。求大神指点!
相关文章推荐
- 关于Android耳机插拔的问题(ACTION_HEADSET_PLUG )
- Android学习之创建中间缓存文件,处理文件名重复问题
- Android:scrollview嵌套listview运行后显示的位置不在顶部而是中间问题
- android 4.0.3 usb插拔提示音播放问题分析
- android中ScrollView嵌套滑动开关问题(上下滑动开关球挺在中间)
- Android WebView加载Html文本不能适配,以及图片中间有空白的问题
- Android 百度SDK由3.3升级到4.3中间问题解决
- eclipse中无法新建Android工程 出现问题:Plug-in org.eclipse.ajdt.ui was unable to load
- Android scrollview嵌套listview运行后最先显示出来的位置不在顶部而是中间问题
- Android 调用系统相机拍照并且显示在相册中,以及中间可能会遇到的一些问题的解决
- android 4.0.3 usb插拔提示音播放问题分析
- coco2d-x 交叉编译成Android程序及中间遇到的问题
- Androidk开发 —— 解决WIFI断线问题
- Android TableRow 的坑,内部view需添加weight才可解决自适应问题
- android out of memory oom问题
- 【游戏开发备注之一】解决Cocos2dx_v1.x版本图片白块,编译Android闪退等问题 推荐
- Android 4.0.4系统在线升级实现过程中的一些问题总结
- Android studio开多个窗口引起的问题
- Android WebView开发常见问题
- android旋转动画卡顿问题