android 使用单例还是service?
来源:互联网 发布:手机免费网络电话软件 编辑:程序博客网 时间:2024/06/05 05:49
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.
http://stackoverflow.com/questions/3567667/android-when-to-use-service-vs-singleton
- android 使用单例还是service?
- Android:使用Service还是Thread
- Android开发实践:使用Service还是Thread
- Android开发实践:使用Service还是Thread
- Android开发实践:使用Service还是Thread
- Android开发实践:使用Service还是Thread
- Android开发实践:使用Service还是Thread
- Android开发实践:使用Service还是Thread
- Android开发实践:使用Service还是Thread
- Android开发实践:使用Service还是Thread
- Android开发实践:使用Service还是Thread
- Android技巧:使用Service还是Thread
- Android开发中使用Service还是Thread
- Android开发中使用Service还是Thread?
- Android开发实践:使用Service还是Thread
- Android开发实践:使用Service还是Thread
- Android开发实践:使用Service还是Thread
- Android开发实践:使用Service还是Thread
- 如何让发布的C#的DLL类库中的方法带有注释说明
- Redis安装及简单测试
- Android 异步查询框架AsyncQueryHandler的使用
- zTree实现地市县三级级联DAO接口
- 随写。
- android 使用单例还是service?
- 阅读程序1
- hiernate xml 和注释配置 (声明式和显示式)
- 编程思想基本概念
- 程序阅读2
- Codeforces Round #243 (Div. 1) A Sereja and Swaps
- 【蓝桥杯】分糖果(2014预赛java-B7)
- ClientDataSet加上TDataSetProvider的数据保存问题
- 不知道做了什么,电脑好卡。。。讲讲Socket,我不太懂,却开始懂得知识