当前位置: 首页 > news >正文

杭州手机网站制作公司西安计算机培训机构

杭州手机网站制作公司,西安计算机培训机构,杭州定制网站公司,网站建设费用有哪些原文地址#xff1a;https://developer.android.com/training/run-background-service/report-status.html 这节课主要学习如何将IntentService中的执行结果返回给请求点。一种推荐的方式就是使用 LocalBroadcastManager来实现#xff0c;它会将所广播的Intent限制在APP内部…原文地址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 its registered to receivepublic 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 filters 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广播。
http://www.zqtcl.cn/news/650299/

相关文章:

  • 自动化优化系统网站建设网站建设类文章
  • 网站建设以及推广提案书支付通道网站怎么做
  • 上海兼职做网站凤凰军事新闻
  • 青田建设局网站ui培训哪好
  • 佛山网站seo哪家好全返网站建设
  • 快速建站哪个平台好常见网页设计
  • 织梦网站地图模板网站服务费
  • 织梦建设两个网站 视频互联网公司排名1000
  • 广州企业网站设计西昌手机网
  • 一个工厂做网站有用吗wordpress重写登录页面
  • 网站服务器如何搭建网站分页设计
  • 可以直接进入网站的正能量连接温州注册网络公司
  • 清丰网站建设价格福州绿光网站建设工作室
  • 武城网站建设价格东莞容桂网站制作
  • 工作室网站需要备案吗wordpress群发工具
  • 官方网站娱乐游戏城自己做网站的好处
  • 查询建设规范的网站1元网站建设精品网站制作
  • 社交网站的优点和缺点个人网页制作软件
  • 做一家算命的网站有没有专门做淘宝客的网站
  • 网站站点管理在哪里建筑施工图设计
  • 众筹网站开发周期网页云原神
  • 哪些网站可以免费做h5东莞制作企业网站
  • 帝国cms 网站地址设置深圳住房和建设部网站
  • 专业网站建设价格最优网页游戏大全电脑版在线玩
  • 建设租车网站wordpress+js插件开发
  • 定制网站开发与模板商务酒店设计网站建设
  • php 网站部署后乱码wordpress禁止调用头部
  • 网站权重低营销型企业网站建站
  • 大港油田建设网站长春市网站优化公司
  • 嘉峪关市建设局建管科资质网站室内设计入门教程