在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ínzà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ànzhōngzhíxíngdecāozuòyǒuzhòngyàoyǐngxiǎng响 : rènyàocāozuòdecāozuòdōuyòng用 , yīnwèinínyàocónghánshùfǎnhuíchǔcāozuò作 , dànshí时 , B r o a d c a s t R e c e i v e r zàichǔhuódòngzhuàngtài态 , bìngqiěyīn此 , zàicāozuòwánchéngzhīqián前 , tǒngyóudezhōngzhǐjìnché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.

Android中的广播基本实现及回调方法的理解的更多相关文章

  1. Android中的广播

    Android中的广播 广播接受器,可以比喻成收音机.而广播则可以看成电台. Android系统内部相当于已经有一个电台 定义了好多的广播事件,比如外拨电话 短信到来 sd卡状态 电池电量变化... ...

  2. Android 中的广播(Broadcast)

    Android 广播(broadcast) 饮水思源 本文章内容学习和总结自 郭霖大神:<Android第一行代码> Overview 就像我们的学校里的喇叭一样,是用来通知的.而Andr ...

  3. Android 中的广播机制

    Android 中的广播机制 Android 中的广播,按照广播响应范围,可以分为应用内广播和全局广播.按照广播的接收方式,可以分为标准广播和有序广播. 广播的分类 响应范围 应用内广播:此类广播只能 ...

  4. Android中的广播Broadcast详解

    今天来看一下Android中的广播机制,我们知道广播Broadcast是Android中的四大组件之一,可见他的重要性了,当然它的用途也很大的,比如一些系统的广播:电量低.开机.锁屏等一些操作都会发送 ...

  5. android中的坐标系以及获取坐标的方法

    android中有两种坐标系,分别称之为Android坐标系和视图坐标系.而对应的也有一些相关的方法可以获取坐标系中的 坐标值.只有搞清楚这些区别,才能在实现的时候不至于出错或者得不到你想要的效果. ...

  6. android中Bitmap的放大和缩小的方法

    android中Bitmap的放大和缩小的方法 时间 2013-06-20 19:02:34  CSDN博客原文  http://blog.csdn.net/ada168855/article/det ...

  7. Android中的Parcelable接口和Serializable使用方法和差别

    Parcelable接口: Interface for classes whose instances can be written to and restored from a Parcel. Cl ...

  8. android中的广播接收实现总结

    1 首先根据广播应用内接收和应用外接收,分两个类进行管理[1]  LocalBroadcastManager,应用内广播管理类[2]  BroadcastManager  广播管理类(部分应用内,应用 ...

  9. Android中使用广播机制退出多个Activity

    谷歌百度一下,Android中退出多个Activity的方法,大家讨论的很多. 在实习的时候,看到公司的项目退出多个Activity,是采用LinkedList方法,毕业设计的时候,也参照了那种方法. ...

随机推荐

  1. 线性表->链式存储->双向链表

    文字描述 之前的链表(单链表.循环链表)的链式存储结构中只有一个指示直接后继的指针域.由此,从某个结点出发只能顺指针往后寻查其他结点.若要寻查结点的直接前驱,则需从表头指针出发.即单链表中,NextE ...

  2. Appium自动化测试之Appium的安装与配置

    Appium自动化测试- Appium环境的安装与配置Appium中文文档: https://github.com/appium/appium/tree/master/docs/cn Appium的安 ...

  3. MFC 修改标题

    1. Overwrite CMainFrame::PreCreateWindow. Clear the style FWS_ADDTOTITLE cs.style &= ~(LONG)FWS_ ...

  4. VUE-003-前端表格数据展示时,设置单元格(el-table-column)保留空格和换行

    在使用 el-table 展示数据时,单元格中的数据有可能存在空格和换行符,若不进行设置,浏览器默认会取消空格和换行符,如下所示: 解决方法: 将单元格的样式 “white-space” 属性设置为“ ...

  5. 设置光标聚焦输入框(EditText)并弹出软键盘(在适配器中设置)

    参考代码: public void setFocusEditTextAndShowSoftInput(final EditText editText){ editText.setFocusable(t ...

  6. python 循环结构 while for...in

    # ### 循环结构 while for...in """ 循环结构的特点:减少代码的冗余,提高代码的效率 语法形式: """ # 打印1~ ...

  7. vi命令设置行号

    1. :set nu :显示行号

  8. selenium常用操作

    1.访问页面获得源码 browser.get(url) browser.page_source 2.查找单个元素:返回一个标签 find_element_by_id  ==>id选择器 find ...

  9. std::vector<bool> 在 auto 推断下的返回值是 bool & 引用

    转自: https://www.cnblogs.com/hustxujinkang/p/5218148.html //////////// std::vector<bool> featur ...

  10. IdeaJ 常见插件安装, 常用配置,常用快捷键

    -- 系统是 Ubuntu 16.04 1, 插件: 2, 常见的设置: [1] 代码提示的修改: File --> settings --> Keymap --> MainMenu ...