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

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


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


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


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


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

各种去 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,描述如下:


Set the enabled setting for a package component (activity, receiver, service, provider). This setting will override any enabled state which may have been set by the component in its manifest.

觉得可行就,先得到receiver这个组件后把他的enable制成false,广播关闭了。but下次程序启动的时候广播木有了,真的木有了啊~~~百思不得其解

最终把AndroidManifest里receiver的enable属性显示声明成true,这次可以了。

觉得之前如果把在AndroidManifest里把application的enable属性也显示声明成true,也应该可以,不过没有试。



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值