android广播与回调,Android中的广播基本实现及回调方法的理解

在Android中broadcast这一节的内容其实不算多主要是牵扯到一个broadcastreceiver类,这个类是一个抽象类,下面有一个抽象方法onreceiver(),可以再我们收到网络状态变化时执行,改抽象方法是一个回调方法,说道回调方法一般只能是抽象方法或者是接口,因为回调方法就是为了让父类或者其他类能调用一个统一的方法(主要是方便其他类),这就像是c+中的动态联编。不同的子类有不同的实现。可以看博客https://blog.csdn.net/chenyefei/article/details/78558974。好了回到broadcastreceiver类,看看api中对他的Lifecycle的解释:BroadcastReceiver对象仅在调用OnReceive(android.content.context,android.content.intent)期间有效。一旦您的代码从此函数返回,系统将认为该对象已完成且不再处于活动状态。这对您在onReceive(android.content.context,android.content.intent)实现中可以执行的操作有重要影响:任何需要异步操作的操作都不可用,因为您需要从函数返回以处理异步操作,但此时,BroadcastReceiver不再处于活动状态,并且因此,在异步操作完成之前,系统可以自由地终止其进程。特别是,您不能显示一个对话框或从广播接收器中绑定到服务。对于前者,您应该使用notificationmanager API。对于后者,可以使用context.startService()向服务发送命令。这说明broadcastreciver类是在onrecive方法回调之前存活,但是为什么broadcastreciver类会自动回调onrevive方法我在broadcastreciver类的源码中找了一下并没有哪个方法调用了onrecive方法所以,我并不明白onrecive是怎么自动回调的。总之在重写onrecive方法时实现了具体的功能,然后就是注册了调用registerreciver方法注意这个方法至少需要一个intentfilter实例和一个broadcastreciver实例。然后在activity的ondestory方法中要unregisterreciver方法注销。这就是大概的过程了还是很简单的。

This has important repercussions to what you can do in an onReceive(android.content.Context, android.content.Intent) implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point the BroadcastReceiver is no longer active and thus the system is free to kill its process before the asynchronous operation completes.

zhè这 duì对 nín您 zài在 o n R e c e i v e ( a n d r o i d . c o n t e n t . c o n t e x t , a n d r o i d . c o n t e n t . i n t e n t ) shí实 xiàn现 zhōng中 kě可 yǐ以 zhí执 xíng行 de的 cāo操 zuò作 yǒu有 zhòng重 yào要 yǐng影 xiǎng响 : rèn任 hé何 xū需 yào要 yì异 bù步 cāo操 zuò作 de的 cāo操 zuò作 dōu都 bù不 kě可 yòng用 , yīn因 wèi为 nín您 xū需 yào要 cóng从 hán函 shù数 fǎn返 huí回 yǐ以 chǔ处 lǐ理 yì异 bù步 cāo操 zuò作 , dàn但 cǐ此 shí时 , B r o a d c a s t R e c e i v e r bù不 zài再 chǔ处 yú于 huó活 dòng动 zhuàng状 tài态 , bìng并 qiě且 yīn因 cǐ此 , zài在 yì异 bù步 cāo操 zuò作 wán完 chéng成 zhī之 qián前 , xì系 tǒng统 kě可 yǐ以 zì自 yóu由 de地 zhōng终 zhǐ止 qí其 jìn进 chéng程 。

In particular, you may not show a dialog or bind to a service from within a BroadcastReceiver. For the former, you should instead use the NotificationManager API. For the latter, you can use Context.startService() to send a command to the service.

标签:zh,ng,content,广播,zu,Android,回调,方法,android

来源: https://www.cnblogs.com/wyx66688/p/10532587.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值