停止Service的stopSelf(int)
来源:互联网 发布:知乎显示器护眼选择 编辑:程序博客网 时间:2024/05/24 04:58
每一个线程都有自己的生命周期,也就是说,如果当前Service不占用太多内存,系统不会杀死这个Service,onStartCommand()执行完之后,Service可以继续跑,Service有两种停止方式:
1. 自己调用stopSelf();
2. 组件调用stopService().
只要有一个stopSelf()或stopService()请求停止Service,那么系统会迅速杀死这个Service。
然而,如果你的Service要同时处理多个请求,你就不能在当前一个请求处理完成之后立刻停止Service,因为很可能现在你已经收到了一个新的启动Service请求(如果立刻停止,那么新来的请求就会跟着终止)。为了避免这种情况发生,你可以用stopSelf(int)来保证你当前停止Service的请求是基于上一个请求的。也就是说,当你调用stopSelf(int),你把startID传给了对应的要停止的Service,这个startID是上一个请求的StartID!!如果没有第二个请求来,那么这个Service就会死掉,但是如果这个Service已经又接受到一个新的启动请求之后,你才调用stopSelf(int),那么你传递给stopSelf()的ID是上一个请求的ID,而当前Service的startID已经更新为新的请求的ID,造成两个ID不对应,stopSelf()失效,那么Service就不会停止。这样就避免了将后面的请求终止。
0 0
- 停止Service的stopSelf(int)
- android Service stopSelf(int startId)与stopSelf()的区别
- android Service stopSelf(int startId)与stopSelf()的区别
- 使用Service的stopSelf()方法来停止服务
- android Service的stopSelf
- Service中stopSelf(int startId)说明
- android Service的stopself方法
- stopSelf() vs stopSelf(int) vs stopService(Intent)
- Android Service stopself方法
- #android# service stopself() stopservice()
- stopSelf()方法的详解
- 【android】在Service的onStartCommand()中调用stopself()应该注意的问题
- stopSelf()方法的使用注意
- 服务(Service)的停止和启动
- Service之“停止Service”
- 关于StopSelf
- Android Service :开始停止service
- 启动和停止Service
- 33 - Search in Rotated Sorted Array
- POJ 2229 Sumsets(dp)
- Android微信支付的两大坑点
- 扒扒数据库长长知识(下载资源组合看day2)之 07 (子查询重点+难点)(一般子查询)
- delphi三层程序,用的scktsrvr.exe。 如何判断客户端是否在线?
- 停止Service的stopSelf(int)
- 判断复选框是否被选中
- ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
- OpenSSH, ssh成功 sftp失败的可能原因
- sys 忘密码
- 【转载】android中MVP模式
- Android内存泄漏分析及实践(二)
- 自定义时间组件
- 浅谈 MVP in Android