读书人

静态登记的receiver也是可以关闭滴

发布时间: 2012-07-15 20:20:06 作者: rapoo

静态注册的receiver也是可以关闭滴

最近做了个demo,用到广播,目的是监听蓝牙连接是否断开。断开时,弹出对话框告知已断开。


开始的时候当连接断开时发出警告用的是AlertDialog,这个东西必须是由activity创建,而我有多个activity。于是就悲催的在N个Activity里注册and销毁receiver(这是有多彪啊)。。。。


而当你真正收到连接断开的时候其实早800年前就被断开了,这个时间差内,再读写之类的会有异常,这时就要有自定义广播~~于是我凌乱了。


凌乱之后,又自定义过滤器,静态注册接收器。这时AlertDialog的问题出现了。我又不淡定了,又是一阵凌乱后,发现再写个activity设置成dialog,再设置透明神马的不就OK(之前是有多蠢啊)。当收到广播的时候转到alertactivity,为了避免多次弹出对话框,在AndroidManifest.xml里设置了单例。


本以为这就完了,但是发现这个静态注册的广播在进程都kill了还在那给我提示静态登记的receiver也是可以关闭滴,疯了。。。

各种去groups.google上搜贴无果后各种看api,发现AndroidManifest中receiver里的enable属性,属性描述如下:


Whether or not the broadcast receiver can be instantiated by the system — "true" if it can be, and "false" if not. The default value is "true".The?<application>?element has its own?enabled?attribute that applies to all application components, including broadcast receivers. The?<application>?and?<receiver>?attributes must both be "true" for the broadcast receiver to be enabled. If either is "false", it is disabled; it cannot be instantiated.


水平太烂凑活瞎理解吧。。想着干脆得到PackageManager后直接把application的enable制成false,结果是收不到广播了,程序也再启动不起来了(这个应该可以解决,下面再说)。。,无奈~


继续看,看到setComponentEnabledSetting,描述如下:




?

读书人网 >移动开发

热点推荐