频道栏目
首页 > 资讯 > Android > 正文

Android开发之广播的基本使用说明

18-07-30        来源:[db:作者]  
收藏   我要投稿

1、Android广播发送及广播类型

广播发送的基本代码:

Intent intent =  new Intent();
intent.setAction(Constant.WAIT_BROADCAST_ACTION);
context.sendBroadcast(intent);

根据广播的发送方式,可以将其分为以下几种类型:

1)普通广播 Normal Broadcast

2)系统广播 System Broadcast

3) 有序广播 Ordered Broadcast

4)粘性广播 Sticky Broadcast (在Android 5.0/Api 21中不推荐使用,有序粘性广播也已经不推荐使用)

5)应用内广播 Local Broadcast(使用LocalBroadcastManager,只能在应用内使用,不能跨进程使用。)

1)普通广播 Normal Broadcast

开发者自己定义的intent,以context.sendBroadcast(...)或者context.sendBroadcastAsUser(...)形式发送。

具体可以使用的方法有:

sendBroadcast(...):

①sendBroadcast(@RequiresPermission Intent intent);

②sendBroadcast(@RequiresPermission Intent intent, @Nullable String receiverPermission);

③sendBroadcast(Intent intent, @Nullable String receiverPermission, @Nullable Bundle options);

④sendBroadcast(Intent intent, String receiverPermission, int appOp);

sendBoadcastAsUser(...)

①sendBroadcastAsUser(@RequiresPermission Intent intent, UserHandle user);

②sendBroadcastAsUser(@RequiresPermission Intent intent, UserHandle user, @Nullable String receiverPermission);

③sendBroadcastAsUser(@RequiresPermission Intent intent, UserHandle user, @Nullable String receiverPermission, int appOp);

2)系统广播 System Broadcast

Android系统内置了多个广播,涉及到手机的基本操作,基本上都会发出相应的系统广播。

系统广播在系统内部当特定事件发生时,由系统发出。

3)有序广播 Ordered Broadcast

有序广播是针对广播接收者(BroadcastReceiver)而言,按照先后顺序接收的。

先后顺序判断标准为:按照Priority由大到小排序;有相同priority的动态广播和静态广播,动态广播会排在前面。

4)粘性广播(Sticky Broadcast)

已经在Android 5.0/Api 21中不推荐使用

5)应用内广播(Local Broadcast)

用LocalBroadcastManager统一处理APP应用内的广播问题。

Android广播可以跨进程甚至跨APP发送,会造成一下问题:

①其他应用可能会针对当前APP,发送与当前APP intent-filter一致的广播,导致APP不断的接收和处理;

②其他应用可以注册与当前APP intent-filter一致的广播,用于接收广播信息。

以上两种情况都存在安全隐患,常见的增加安全性的方案是:

①本地APP内部发送和接收的广播,将exported属性设为false。

②在发送和接收广播时,增加上相应的permission,用于权限验证。

③发送广播时,指定广播接收器所在的包名

2、广播发送和接收的原理

除应用内广播,其他广播发送和接收原理

①继承BroadcastReceiver,重写onReceive()方法;

②通过Binder机制向ActivityManagerService注册广播

③通过Binder机制向ActivityManagerService发送广播

④AMS查找符合条件(IntentFilter/Permission)的BroadcastReceiver,将广播发送到BroadcastReceiver所在的消息队列中。

⑤BroadcastReceiver所在的队列收到此广播后,回调onReceive()方法。

应用内广播,发送和接收原理与普通广播基本一致,将ActivityManagerService换为LocalManagerService控制广播的接收和发送等流程。

3、广播数据的限制(未验证,待验证)

广播传输数据大小应该尽可能的小。不能太大。图片或者太长的字符串应该以别的方式传递。

进程内,可以用EventBus,文件缓存,磁盘缓存。

官网上对TransationTooLargeException有如下说明:

TransactionTooLargeException

The Binder transaction failed because it was too large.

During a remote procedure call, the arguments and the return value of the call are transferred asParcelobjects stored in the Binder transaction buffer. If the arguments or the return value are too large to fit in the transaction buffer, then the call will fail andTransactionTooLargeExceptionwill be thrown.

The Binder transaction buffer has a limited fixed size, currently 1Mb, which is shared by all transactions in progress for the process. Consequently this exception can be thrown when there are many transactions in progress even when most of the individual transactions are of moderate size.

There are two possible outcomes when a remote procedure call throwsTransactionTooLargeException. Either the client was unable to send its request to the service (most likely if the arguments were too large to fit in the transaction buffer), or the service was unable to send its response back to the client (most likely if the return value was too large to fit in the transaction buffer). It is not possible to tell which of these outcomes actually occurred. The client should assume that a partial failure occurred.

The key to avoidingTransactionTooLargeExceptionis to keep all transactions relatively small. Try to minimize the amount of memory needed to create aParcelfor the arguments and the return value of the remote procedure call. Avoid transferring huge arrays of strings or large bitmaps. If possible, try to break up big requests into smaller pieces.

意思大概是说

通过Binder缓冲区中的Parcel对象进行传输。Binder事务缓冲区具有有限的固定大小,当前为1Mb。

由当前进程正在进行的所有事务共享。因此,即使大多数单个事务的大小适中,当有许多事务正在进行时,也会抛出此异常。

当抛出TransactionTooLargeException时,有两种可能得原因。客户端无法将其请求发送到服务(可能因为参数太大而无法保存到事务缓冲区中),或者服务无法将其响应发送回客户端(可能因为返回值为太大而无法保存到事务缓冲区中)。

避免TransactionTooLargeException的关键是保持所有事务相对较小。

避免传输大量字符串或大位图。如果可能的话,尝试将大量请求分解成更小的部分。

相关TAG标签
上一篇:一代人只有一次机遇的数字建筑
下一篇:关于hive函数返回的数据类型浅谈sparksql
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站