编程注册的广播接收器 [英] Programmatically register a broadcast receiver

查看:218
本文介绍了编程注册的广播接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是编程方式注册的广播接收器的最佳实践/方法。我想注册特定的接收器可根据用户的选择。

I'd like to know what is the best practice/way of programmatically register a broadcast receiver. I want to register specific receivers according to user choice.

作为注册是通过清单文件做了,我不知道是否有一个适当的方式在code来实现这一点。

As the registration is done through the manifest file, I'm wondering if there's a proper way to achieve this in code.

推荐答案

这听起来像你想控制的组件是否刊登在你的清单是积极的,而不是动态注册一个接收器(通过Context.registerReceiver())在运行。

It sounds like you want to control whether components published in your manifest are active, not dynamically register a receiver (via Context.registerReceiver()) while running.

如果是这样,你可以使用PackageManager.setComponentEnabledSetting()来控制这些设备是否处于活动状态:

If so, you can use PackageManager.setComponentEnabledSetting() to control whether these components are active:

<一个href="http://developer.android.com/reference/android/content/pm/PackageManager.html#setComponentEnabledSetting%28android.content.ComponentName,%20int,%20int%29">http://developer.android.com/reference/android/content/pm/PackageManager.html#setComponentEnabledSetting(android.content.ComponentName, INT,INT)

请注意,如果你只关注接收广播你正在运行时,最好是使用registerReceiver()。接收机组件是当你需要确保你的应用程序是广播发送,每次推出的主要是有用的。

Note if you are only interested in receiving a broadcast while you are running, it is better to use registerReceiver(). A receiver component is primarily useful for when you need to make sure your app is launched every time the broadcast is sent.

这篇关于编程注册的广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆