Android官方开发文档Training系列课程中文版:后台服务之响应IntentService的处理结果
原文地址:https://developer.android.com/training/run-background-service/report-status.html
这节课主要学习如何将IntentService中的执行结果返回给请求点。一种推荐的方式就是使用 LocalBroadcastManager来实现,它会将所广播的Intent限制在APP内部。
发送IntentService的处理结果
为了可以将IntentService的处理结果发送给其它组件,首先需要创建一个Intent对象,并将执行结果放入该Intent内。
接下来要做的就是:将刚才创建好的Intent通过LocalBroadcastManager.sendBroadcast()发送出去。但凡是对该Intent注册了的,那么发送该Intent都会收到结果。可以通过getInstance()获取LocalBroadcastManager的实例。
例子如下:
public final class Constants {...// Defines a custom Intent actionpublic static final String BROADCAST_ACTION ="com.example.android.threadsample.BROADCAST";...// Defines the key for the status "extra" in an Intentpublic static final String EXTENDED_DATA_STATUS ="com.example.android.threadsample.STATUS";...
}
public class RSSPullService extends IntentService {.../** Creates a new Intent containing a Uri object* BROADCAST_ACTION is a custom Intent action*/Intent localIntent =new Intent(Constants.BROADCAST_ACTION)// Puts the status into the Intent.putExtra(Constants.EXTENDED_DATA_STATUS, status);// Broadcasts the Intent to receivers in this app.LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
...
}
下一步就是如何处理接收到的Intent对象了。
接收IntentService的处理结果
如果需要接收广播出来的Intent,那么就需要用到BroadcastReceiver了。在BroadcastReceiver的实现类中重写onReceive()方法。当LocalBroadcastManager将相应的Intent对象广播出来后,那么该方法就会被自动回调。
举个例子:
// Broadcast receiver for receiving status updates from the IntentService
private class ResponseReceiver extends BroadcastReceiver
{// Prevents instantiationprivate DownloadStateReceiver() {}// Called when the BroadcastReceiver gets an Intent it's registered to receive@public void onReceive(Context context, Intent intent) {
.../** Handle Intents here.*/
...}
}
一旦定义好了BroadcastReceiver,那么就可以为其定义指定的意图过滤器了。要做到这些,需要创建一个IntentFilter。下面的代码演示了如何定义一个过滤器:
// Class that displays photos
public class DisplayActivity extends FragmentActivity {...public void onCreate(Bundle stateBundle) {...super.onCreate(stateBundle);...// The filter's action is BROADCAST_ACTIONIntentFilter mStatusIntentFilter = new IntentFilter(Constants.BROADCAST_ACTION);// Adds a data filter for the HTTP schememStatusIntentFilter.addDataScheme("http");
为了将BroadcastReceiver以及IntentFilter注册到系统,需要先获取LocalBroadcastManager的实例,然后再调用它的registerReceiver()方法。下面的代码演示了这个过程:
// Instantiates a new DownloadStateReceiverDownloadStateReceiver mDownloadStateReceiver =new DownloadStateReceiver();// Registers the DownloadStateReceiver and its intent filtersLocalBroadcastManager.getInstance(this).registerReceiver(mDownloadStateReceiver,mStatusIntentFilter);...
BroadcastReceiver可以同时处理多种类型的Intent对象,这项特性可以为每种Action定义不同的代码,而不需要专门去定义BroadcastReceiver。为同一个BroadcastReceiver定义另外的IntentFilter,只需再创建一个IntentFilter,然后再次注册一下就好:
/** Instantiates a new action filter.* No data filter is needed.*/statusIntentFilter = new IntentFilter(Constants.ACTION_ZOOM_IMAGE);...// Registers the receiver with the new filterLocalBroadcastManager.getInstance(getActivity()).registerReceiver(mDownloadStateReceiver,mIntentFilter);
发送广播Intent并不会启动或者恢复Activity。就算是APP处于挂起状态(处于后台)也同样会接收到Intent。如果APP处于挂起状态的话,有任务完成需要通知到用户,那么可以使用Notification做到。绝不要启动Activity来响应接收到的Intent广播。
Android官方开发文档Training系列课程中文版:后台服务之响应IntentService的处理结果相关推荐
- Android官方开发文档Training系列课程中文版:目录
原文地址 : http://android.xsoftlab.net/training/index.html 引言 在翻译了一篇安卓的官方文档之后,我觉得应该做一件事情,就是把安卓的整篇训练课程全部翻 ...
- Android官方开发文档Training系列课程中文版:创建自定义View之View的创建
原文地址:http://android.xsoftlab.net/training/custom-views/index.html 引言 Android框架含有大量的View类,这些类用来显示各式各样 ...
- Android官方开发文档Training系列课程中文版:OpenGL绘图之图形绘制
原文地址:http://android.xsoftlab.net/training/graphics/opengl/draw.html 如果你还不清楚如何定义图形及坐标系统,请移步:Android官方 ...
- Android官方开发文档Training系列课程中文版:使用Fragment构建动态UI之Fragment创建
原文地址:http://android.xsoftlab.net/training/basics/fragments/index.html 导言 为了在Android中创建动态的多面板用户界面,你需要 ...
- Android官方开发文档Training系列课程中文版:电池续航时间优化之监测电池电量及充电状态
原文地址:http://android.xsoftlab.net/training/monitoring-device-state/index.html 引言 作为一款优秀的APP应用,应该总是想方设 ...
- Android官方开发文档Training系列课程中文版:管理设备的睡眠状态
原文地址:http://android.xsoftlab.net/training/scheduling/index.html 引言 当Android设备处于闲置状态时,它的屏幕首先会变暗,接着会关闭 ...
- Android官方开发文档Training系列课程中文版:电池续航时间优化之检查、检测网络连接状态
原文地址:http://android.xsoftlab.net/training/monitoring-device-state/connectivity-monitoring.html 通常会有一 ...
- Android官方开发文档Training系列课程中文版:APP的内存管理
写在开头的话: 如果有同学对Android性能比较关注的,可以阅读这篇文章:Android性能优化建议 原文地址:http://android.xsoftlab.net/training/articl ...
- Android官方开发文档Training系列课程中文版:后台服务之IntentService的创建
原文地址:http://android.xsoftlab.net/training/run-background-service/index.html 引言 除非特别指定,否则所有的操作都是在UI线程 ...
- Android官方开发文档Training系列课程中文版:电池续航时间优化之检查、检测网络连接状态...
原文地址:http://android.xsoftlab.net/training/monitoring-device-state/connectivity-monitoring.html 通常会有一 ...
最新文章
- NetCore学习实践1__项目创建和配置文件的获取
- 贝叶斯统计:Inverted Beta与Three Parameter Beta分布
- 蓝牙a2dp硬件卸载是什么意思_索尼这项音频黑科技 让蓝牙音质从此不输有线
- mac下flink集群安装
- LGD模型开发细节|全网首发
- 自己实践的mac安装python3Linux安装python3
- 用足球阵型告诉你,阿里云如何护航全网70%世界杯流量 1
- 使用Supervisor让你的Swift Perfect服务器项目后台运行
- CTRL+ALT快捷键汇总
- 英文翻译软件哪个好?不能错过的有这几个。
- 一个三非渣本的安卓秋招之路
- 小细节见实力,告诉你vivo Z3如何成为爆款千元机
- 数字孪生开启传统行业数字化转型升级之路
- 《历术甲子篇》冬至合朔表
- unity 四元数和欧拉角的相互转换
- iOS GameCenter 挑战,排名
- 数据结构——存储结构和逻辑结构
- solidity投票合约在bcos上的部署及解析(三)
- Unity Direct3D 和 OpenGL
- 国际标准刊号(ISSN)
热门文章
- STL中vectortype的复制
- linux spinlock/rwlock/seqlock原理剖析(基于ARM64)
- Linux 内核通知链和例程代码
- java 数组正则表达式_java正则表达式实现提取需要的字符并放入数组【ArrayList数组去重复功能】...
- semihost/ITM机制浅析以及使用JLINK通过ITM调试stm32单片机
- Datawhale-零基础入门NLP-新闻文本分类Task01
- redis rdb aof区别_Redis 持久化之 RDB 与 AOF 详解
- 鸿蒙系统能不能用了,【图片】华为鸿蒙系统的厉害之处在于 你可能非用不可
!【手机吧】_百度贴吧...
- 三、Express 路由
- 解决后端返回数据中的大数字问题(使用第三方包json-bigint )