- 7
- 0
- 约8.22千字
- 约 9页
- 2017-07-09 发布于河南
- 举报
android server介绍
Android Service
所谓的service有Local Service和Remote Service区分:
LocalService:就是client和Service在同一个进程当中。
RemoteService:就是client 和Service在不同的进程中。
我们通常的用法是自己建一个.java文件中通过继承Service来实现一个子Service。然后在通过其他的Activity来连接实现的那个Service就有点像网络编程中的链接服务器一样。但是这里的Service不是独立的一个服务器端,它可以说是手机app的一个实现模块。所以不是像传统的网络编程一样,首先启动服务器,然后在从client去访问。android中的Service要通过访问端通过传递消息(Intent)去开启Service。通常有两种方法去启动一个Service。
一)context.startService()启动方式
public abstract ComponentName startService(Intent service)
通过函数的原型我们可以看出是一个Component去调用的,参数就是一个Intent,在该Intent中去指定一些规则和消息来启动符合条件的Service。
The Intent can either contain the complete class name of a specific service implementation to start, or an abstract definition through the action and other fields of the kind of service to start(by gg doc).
此时我们明确了client端发出去Intent去启动Service的过程,但是Service端是怎么来响应的?然后怎么启动你实现的子Service的呢?
Every call to this method(startService(Intent intent)) will result in a corresponding call to the target services
onStartCommand(Intent, int, int)(by gg doc)就是each component调用startService(Intent intent)来启动Service的时候,service端作出的响应就是call onStartCommand(Intent,int,int)函数
public int onStartCommand (Intent intent, int flags, int startId)
当调用该函数的时候intent对象就是startService(Intent intent)红传递过来的intent对象。flags:start request的额外请求,可以是:0, START_FLAG_REDELIVERY,START_FLAG_RETRY.
startId:an unique integer 来展示这次请求
目前gg doc给出了四种返回值类型。
START_STICKY:如果service进程被kill掉,保留service的状态为开始状态,但不保留递送的intent对象。随后系统会尝试重新创建service,由于服务状态为开始状态,所以创建服务后一定会调用onStartCommand(Intent,int,int)方法。如果在此期间没有任何启动命令被传递到service,那么参数Intent将为null。
START_NOT_STICKY:返回该返回值时,如果在执行完onStartCommand后,Service被一场kill掉后,系统不会重启该Service。
START_REDELIVER_INTENT:但返回这个返回值的时候,从startService传过来的intent对象将保存它的数据,当Service被异常kill掉的后,Service会重启,然后再将Intent传入。
START_STICKY_COMPATIBILITY:START_STICKY的兼容版本,但不保证服务被kill后一定能重启
这个函数不能人为的去直接调用,而是由Android system在Service的main thread中有系统去调用。
所以一般情况下继承Service的子类都要重写Service类的:OnCreate(),onStartCommand(),onBinder(),onStart(),onDestory(),onRebind(),onUnbind()方法,然后在编写其他自己要是想的功能实现函数。
所以通过
您可能关注的文档
最近下载
- 2019款一汽丰田卡罗拉_汽车使用手册用户操作图解驾驶车主车辆说明书电子版.pdf
- 2026年春季人教版小学数学三年级下册教学计划(含进度表).docx VIP
- 长安三万里 中英文字幕.docx VIP
- 长安三万里中英文字幕.pptx VIP
- 早孕关爱门诊咨询要点专家共识(2025年版).pptx VIP
- 汽车行走的艺术知到智慧树期末考试答案题库2025年吉林大学.docx VIP
- (2026春新版本) 部编版道德与法治三年级下册全册教学设计.docx
- TCQFX001-2024四川省机动车维修工时定额标准.pptx VIP
- ASTM E92-2023中文版,维氏硬度试验.pdf VIP
- Roland罗兰VM-3100PRO MIDI使用说明书.pdf
原创力文档

文档评论(0)