1. Polling. The Controller periodically asks the Service for the latest data. IMHO, this option sucks, but it's certainly possible.

  2. Callbacks. The Controller registers a callback object ("observer") with theService. The Service invokes a method on the callback when the data changes, which in turn updates the UI. You can see an example of using that with a Service here.

  3. Broadcast Intents. The Service broadcasts an Intent via sendBroadcast() on a data change. The Controller registers a BroadcastReceiver using registerReceiver(), and that BroadcastReceiver is notified of an incoming broadcast. This triggers the Controller to load the latest data from the Service, or possibly just to get the latest data out of extras in the broadcast Intent. You can see an example of using that technique with a Service

e.g.

Your onReceive() method in the BroadcastReceiver gets a Context passed, use that.

@Override
publicvoid onReceive(Context context,Intent intent){
db = newDatabase(context);//more stuff
}

Also be aware that inside a BroadcastReceiver, you are only guaranteed 10 seconds of execution time, after that, Android may kill your Receiver. So be quick with what you're doing and if the database operation is lengthy, consider using a separate Thread.

How does controller listen to service?的更多相关文章

  1. 深入理解--SSM框架中Dao层,Mapper层,controller层,service层,model层,entity层都有什么作用

    SSM是sping+springMVC+mybatis集成的框架. MVC即model view controller. model层=entity层.存放我们的实体类,与数据库中的属性值基本保持一致 ...

  2. 非Controller类无法使用Service bean解决方案

      尝试方案: 1 在Spring的配置文件springmvc.xml中,增加扫描项base-package="zxs.ssm.util",增加你需要使用service的类所在的包 ...

  3. controller层,service层,dao层(main函数,子函数,子的子函数)

    controller层相当于main函数————————————————————————————————————————————————————@RequestMapping("/query ...

  4. SpringBoot—单元测试模板(controller层和service层)

    介绍 概述   在开发过程中,我们经常会一股脑的写各种业务逻辑,经常等全部大功告成的时候,打个jar包放环境里跑跑看看能不能通,殊不知在各个业务方法中已经漏洞百出,修复一个打一个包,再继续修复,这种效 ...

  5. 非Controller中调用Service

    1.       新增文件 package com.library.common; import org.springframework.beans.BeansException; import or ...

  6. spring非controller类获取service方法

    ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); pushMessageServ ...

  7. spring mvc中的service和controller中读取不到properties值

    根据web.xml读取配置文件中的顺序来看 controller层和service层来自于spring mvc.xml中读取,所以必须要在spring mvc.xml中配置读取资源文件夹方式

  8. 【起航计划 035】2015 起航计划 Android APIDemo的魔鬼步伐 34 App->Service->Local Service Controller

    Local Service Controller 是将LocalService当作“Started”Service来使用,相对于”Bound” Service 来说,这种模式用法要简单得多,Local ...

  9. SSM集成shiro 致使Controller无法自动注册service

    由于shiro在web.xml中配置属于过滤器,其中在web.xml中的加载顺序为: <context-param>(上下文) > listener > filter > ...

随机推荐

  1. [Tools] 使用XP远程登录Win8系统

    [背景] 完成最基本的设置后,发现xp依然不能远程访问win8桌面,搜索后发现需要进一步设置   [开工] 按照参考资料进行设置,下面的参考资料已经写的很详细了,只是参考资料2中的文件名: redss ...

  2. 安卓app设计规范整理和Android APP设计篇(转)

    随着安卓智能手机不停的更新换代.安卓手机系统越来越完美,屏幕尺寸也越来越大啦!比如最近小米的miui 6的发布和魅族手机系统的更新等等. 以小米MIUI6的安卓手机来说,MIUI6进行了全新设计,坚持 ...

  3. Java学习笔记(十)——多态

    一.多态 1.对象的多种形态 (1)引用多态: 父类的引用可以指向本类的对象 父类的引用可以指向子类的对象 (2)方法多态: 创建本类对象时,调用的方法为本类方法: 创建子类对象时,调用的方法是子类方 ...

  4. Java学习随笔4:Java的IO操作

    1. IO流的作用是读写设备上的数据,如硬盘文件.内存.键盘.网络等.根据数据走向,可分为:输入流和输出流:根据处理的数据类型,可分为:字节流和字符流.字节流可以处理所有类型的数据,如MP3.图片.视 ...

  5. Quick-Cocos2dx 快速了解

    set_dev_envirment // */ // ]]> Cocos2dx快速了解 1. Quick-Cocos2dx 开发环境搭建 1.1. 官网: 1.2. Github: 1.3. s ...

  6. 【hibernate】 hibernate的主键策略

    今天使用maven生成一个sping+springMVC+hibernate 的项目,报错如下: 错误提示呢:不能解释这个id的生成策略[uuid.string].就是uuid.string这个hib ...

  7. 系统剖析Android中的内存泄漏

    [转发]作为Android开发人员,我们或多或少都听说过内存泄漏.那么何为内存泄漏,Android中的内存泄漏又是什么样子的呢,本文将简单概括的进行一些总结. 关于内存泄露的定义,我可以理解成这样 没 ...

  8. JMeter监控Linux服务器资源案例

    JMeter是一款压力测试工具,我们也可以用它来监控服务器资源使用情况.JMeter正常自带可以通过Tomcat的/manager/status来监控服务资源使用情况.这种情况只能监控Tomcat支持 ...

  9. RabbitMQ常用命令

    创建一个用户为mytest,密码为mytest rabbitmqctl add_user mytest mytest 删除一个用户  rabbitmqctl delete_user username ...

  10. Excel: Switch (transpose) columns and rows

    链接:https://support.office.com/en-in/article/Switch-transpose-columns-and-rows-ed1215f5-59af-47e6-953 ...