BroadcastReceiver的动态注册与静态注册 [英] Dynamic Registration vs Static Registration of BroadcastReceiver

查看:176
本文介绍了BroadcastReceiver的动态注册与静态注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们所有人都知道,我们注册了两种类型的 BroadcastReceiver

All of us known we register BroadcastReceiver in two types

1)Static Registration

2)Dynamic Registration

但是我的疑问是何时需要使用Static和何时需要使用Dynamic?

But my doubt is when we need to use Static and when we need to use Dynamic?

推荐答案

我们知道,注册BroadcastReceiver有两种方法.一 是静态,而另一个动态.

As we know there are two ways to register a BroadcastReceiver; one is static and the other dynamic.

静态:

  1. 在清单文件中使用标签. (AndroidManifest.xml)
  2. 并非所有事件都可以静态注册.
  3. 某些事件需要权限.

动态:

  1. 使用Context.registerReceiver()动态注册实例.
  2. 注意:暂停时注销.
  1. Use Context.registerReceiver() to dynamically register an instance.
  2. Note: Unregister when pausing.

当我们进行动态注册时(即在运行时),它将与应用程序的生命周期相关联.如果我们进行静态注册(即在编译时),并且我们的应用未运行,则会创建一个新的流程来处理广播.

When we are doing dynamic registration (i.e. at run time) it will be associated with lifecycle of the app. If we do it static registration (i.e. on compile time) and our app is not running, a new process will be created to handle the broadcast.

这篇关于BroadcastReceiver的动态注册与静态注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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