如果广播接收器内的活动被宣布? [英] Should BroadcastReceiver be declared inside activities?

查看:410
本文介绍了如果广播接收器内的活动被宣布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是更好的清洁设计方法来处理的广播接收器之间创建这两个:


  1. 直接声明一个广播接收器在活动/片段,并有压倒一切的onReceived方法。


  2. 在一包接收器,它扩展了广播接收器类和重写的onReceive创建另一个自定义接收器类。然后,你实例化自定义类的接收器在活动/片段。



解决方案

有2种类型的广播接收机的

1),该活动注册内部动态广播接收机

2)注册或用具体行动接收标签(何时启用接收器)

清单文件中声明静态广播接收器

用途:例如说,收到消息

1)动态广播接收装置,用于主要接收广播意图通过刷新发送邮件发送广播的方法。

2)静态广播接收装置,用于接收message.Here已注册的接收器,短信接收操作将收到消息,后来它发送到活动

最后这取决于开发人员使用哪一种,何时使用基于需求

I'm wondering what's the better cleaner design approach to handle BroadcastReceiver creation between those two:

  1. Declaring directly a BroadcastReceiver in an Activity/Fragment and overriding onReceived method there.

  2. Creating another custom receiver class in a package "receiver", that extends the BroadcastReceiver class and override onReceive. Then, you instantiate that custom receiver class in your Activity/Fragment.

解决方案

There are 2 types of Broadcast Receivers

1)Dynamic Broadcast Receiver that is registered inside activity

2)Static Broadcast Receiver that is registered or declared inside Manifest file with Receiver tag with specific action(when to fire the Receiver)

Uses:Eg say receive message

1) Dynamic Broadcast Receiver is used to receive the Broadcast intent mainly to refresh the message sent through send Broadcast method.

2)Static Broadcast Receiver is used to receive the message.Here the Registered Receiver with SMS receive action will receive the message and later it sent to activity

At last It depends on developer which one to use,when to use based on requirement

这篇关于如果广播接收器内的活动被宣布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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