广播接收器的动态注册与静态注册 [英] Dynamic Registration vs Static Registration of BroadcastReceiver

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

问题描述

我们都知道我们以两种类型注册BroadcastReceiver

All of us known we register BroadcastReceiver in two types

1)静态注册

2)动态注册

但我怀疑什么时候需要使用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. 注意:暂停时取消注册.

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

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.

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

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