stackover看到的回答,挺不错的。

I'm quite new to Android development.

When is it a good idea to create an Android Service instead of just using a simple Singleton class?

Take, for example, the data layer downloading information feeds from the internet.

Using a Service seems too much for some cases but sometimes I might need access to a Context so I'm a little unsure about how to design the app.

1 Answer

If it is okay for your process to be killed (along with the singleton) immediately after the user leaves its activities, then use a singleton. If you need it to continue running for some duration after that, use a service. If you would like to continue running after the user leaves it, but can live with it not because the user is now on to something else where memory is needed more, then use a singleton.

The decision between these two only comes down to the lifecycle of your app. For this purpose, that is all a service does -- ask the platform to modify its management of your process. If you need a context in a singleton, just use Context.getApplicationContext() to retrieve the global context for your process.

4

The last sentence of the first paragraph is a bit confusing. Can you rephrase it a bit? –

chakrit

I think it means: Use a singleton when you would prefer the process to continue running after the user leaves it, but only if memory is not needed more somewhere else. –

mparaz

1

So, for your example of the information feed downloads, I guess if those feeds were buffered on the Internet server that provides them, so that you could catch up whenever the user returned to your app, or if you only cared about processing new items from the feed, and not about displaying an uninterrupted history, then you would be OK with a singleton, but if you needed your app to keep monitoring the feed while the user was doing something else, you would then want a service. Just trying to make this a little more real...interesting question. –

Carl

http://www.voidcn.com/article/p-ralvhwvn-bso.html

android service是单例么,android 使用单例还是service?相关推荐

  1. Android自定义view之网易云推荐歌单界面

    系列文章目录 Android自定义view之网易云推荐歌单界面 文章目录 系列文章目录 前言 一.实现 1.自定义一个圆角图片控件(也可直接使用第三方框架) 2.进行布局摆设 3.图片切换动画效果 二 ...

  2. android 启动其他app的activity,Android在一个app中启动其他app中的service或者Activity

    前言: 启动另一个app的activity和service其实是一样的,区别在于startActivity(intent)还是startService(intent)而已:所以下面案例以启动另一个ap ...

  3. 以Android L读取系统所有logcat并写入文件为例分析Android 以添加系统进程的方式申请selinux的权限执行shell脚本,以及avc:dined应该怎么申请权限

    添加selinux较好的文章,可以通过adb shell dmesg > kenel.log 获取kmesg 可以看到 avc:dined 相关内容 首先说说环境: 基于Android L的ao ...

  4. 百度地图 Android SDK - 检索功能使用的简单演示样例

    百度地图 SDK 不仅为广大开发人员提供了炫酷的地图展示效果.丰富的覆盖物图层,更为广大开发人员提供了多种 LBS 检索的能力. 通过这些接口,开发人员能够轻松的訪问百度的 LBS 数据,丰富自己的移 ...

  5. android 启动服务同时传递数据,Android Studio开发基础之起动Service,并通过从Activity向Service传递数据...

    Android Studio开发基础之启动Service,并通过从Activity向Service传递数据 本实例演示启动Service,并通过从Activity向Service传递数据,新建一个Se ...

  6. Android学习小记-----监听并保存传感器数据,让service后台运行(保持CPU运转

    最近做了一个Demo,监听手机中传感器的数据,并将数据保存到手机文件中,发现数据会有丢失的现象. 经过多次测试,发现系统进入深度休眠了,之后service会停止,虽然增加了service自动启动的功能 ...

  7. android.intent.action.view 融云,Android 融云SDK集成单聊

    一 .下载 SDK 您可以到融云官方网站下载融云 SDK.融云 SDK 各部分功能以插件化的形式独立提供,开发者可以根据自己的需要,自由组合下载.各组件的功能如下: IMKit – 融云 IM 界面组 ...

  8. Android 面试(四):Android Service 你真的能应答自如了吗?

    本次我们没有采取分享公众号推文的方法,我想直接这样看看效果. 本文首发于微信公众号「nanchen」,你可以直接在公众号搜索「nanchen」或者扫描最下面的二维码关注我.做不完的开源,写不完的矫情, ...

  9. 下面不是android四大组件之一的是,Android四大组件之一 Service

    Service是长期在后台运行的没界面的Android四大组件之一,默认是运行在主线程中的,如果在service中执行耗时操作超过20秒没响应的话就会造成ANR,所以可以使用IntentService ...

  10. 既然android service是运行在主线程中的,那service还有什么用?

    既然android service是运行在主线程中的,那service还有什么用? 对于Android,每一个进程都有一个主线程,四大组件的处理任务都是在这个线程中进行的.每个线程都有一个Messag ...

最新文章

  1. 即使有历史最佳年报,安踏的未来依然有三重重担
  2. WORD2010自动编号后,目录那里编号和文字中间有很大的空格,怎么
  3. 美团王兴:24岁创业39岁迎来上市,越对未来有信心,越对当下有耐心
  4. 二进制码转十进制java,Java将二进制转换为十进制
  5. linux查看和编辑文件,查看和编辑文件(Linux的快捷键和主要命令)
  6. CSS魔法堂:hasLayout原来是这样!
  7. 《分布式系统:概念与设计》一2.3.1 体系结构元素
  8. Arts 第九周(5/13 ~ 5/19)
  9. 系统集成项目管理工程师13《项目配置管理》
  10. JPA中Specification方法
  11. UE4-4.26蓝图功能实现:按键控制物体旋转的“延伸”(点名系统Get all actors of class)
  12. 赫兹的单位换算_赫兹单位换算(赫兹的单位换算公式)
  13. 声效表达在游戏沉浸方面的影响力
  14. 你是否还记得有一款游戏叫做“魔塔”?
  15. html移动轮播后盾网,后盾网lavarel视频项目---Vue项目使用vue-awesome-swiper轮播插件...
  16. 【超级简单但超级有用】让PDF书籍变身为可搜索文件
  17. 计算机二级C语言-A程序填空题
  18. FFMPEG音频视频开发: 开发本地视频播放器(单线程解码)
  19. Linux学习5之查找文件中的某个字符串并返回所在行号
  20. 如何通过备份 Windows 7 “ 两个激活文件”实现重装操作系统后的自行激活?

热门文章

  1. easyui-layout中的收缩层无法显示标题问题解决
  2. CentOS7安装配置DNS服务器
  3. 实验十 配置EIGRP协议
  4. Feature Selection Techniques
  5. Java项目中,如何限制每个用户访问接口的次数
  6. 转换时间对象和字符串对象添加时间
  7. python 基础学习3-函数
  8. Python:高级主题之(属性取值和赋值过程、属性描述符、装饰器)
  9. 关于Inflater
  10. 第一次创业失败的总结 - 产品设计