[英]android - how to broadcast receiver is registered or not?
I am creating broadcast receiver and calling from an activity. 我正在创建广播接收器并通过活动进行呼叫。 dynamically i want to check the broadcast receiver is registered or not so that i can unregister the broadcast receiver?
动态地我想检查广播接收器是否已注册,以便我可以注销广播接收器? Thanks in advance
提前致谢
Please check the link below for the answer. 请查看下面的链接以获取答案。 Their you can see a short nice answer:
他们可以看到一个简短的好答案:
Unregistering Android Broadcast Receiver in onReceive throws "Receiver not registered" 在onReceive中取消注册Android Broadcast Receiver会引发“未注册接收器”
have this code to enable and disable the broadcast receiver using package manager: This code works like Toggle button. 使用程序包管理器使此代码启用和禁用广播接收器:此代码的作用类似于“切换”按钮。
int flag = (enabled ?
PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
if(flag == 1)
flag = 2;
else
flag = 1;
Log.d("After Flag","::"+flag);
ComponentName component = new ComponentName(StartActivity.this, SMSReceiver.class);
getPackageManager().setComponentEnabledSetting(component, flag,PackageManager.DONT_KILL_APP);
Where enable is Boolean which I have saved as shared preference to know whether the last time my broadcast receiver was working or not. 其中enable是布尔值,我已将其保存为共享首选项,以了解我的广播接收器上次是否工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.