首页 文章 精选 留言 我的

精选列表

搜索[服务],共10000篇文章
优秀的个人博客,低调大师

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 action public static final String BROADCAST_ACTION = "com.example.android.threadsample.BROADCAST"; ... // Defines the key for the status "extra" in an Intent public 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 instantiation private 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_ACTION IntentFilter mStatusIntentFilter = new IntentFilter( Constants.BROADCAST_ACTION); // Adds a data filter for the HTTP scheme mStatusIntentFilter.addDataScheme("http"); 为了将BroadcastReceiver以及IntentFilter注册到系统,需要先获取LocalBroadcastManager的实例,然后再调用它的registerReceiver()方法。下面的代码演示了这个过程: // Instantiates a new DownloadStateReceiver DownloadStateReceiver mDownloadStateReceiver = new DownloadStateReceiver(); // Registers the DownloadStateReceiver and its intent filters LocalBroadcastManager.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 filter LocalBroadcastManager.getInstance(getActivity()).registerReceiver( mDownloadStateReceiver, mIntentFilter); 发送广播Intent并不会启动或者恢复Activity。就算是APP处于挂起状态(处于后台)也同样会接收到Intent。如果APP处于挂起状态的话,有任务完成需要通知到用户,那么可以使用Notification做到。绝不要启动Activity来响应接收到的Intent广播。

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册