Started Service 与 Bound Service

主要说明Started Service与Bound Service的区别于联系。

一:基本用法

Stared Service
对于一个Component对象,调用startService(Intent aIntent)来启动一个Service

启动之后,在Service里面onStartCommand(Intent intent,int flags,int startId)方法里面的Intent就是startService(Intent aIntent)传入的aIntent对象(startId参数的说明:因为一个Service可能处理多个request,所以多次调用startService()就会产生一个startId,并且是从1开始的。。。。原文如下:you handle each call to onStartCommand()yourself, you can perform multiple requests simultaneously,flags参数的说明:暂时还没搞懂)

Started Service由于是一直在运行,像音乐播放器,可以用前台服务(Service in the Foreground)调用startForeground(int id, Notification notification)即可,System一般在内存不足的时候不会先回收前台服务。因为前台服务有焦点把,我估计是这样的。

Bound Service
对于一个Component对象,调用bindService(Intent aIntent)来绑定一个Service

绑定之后,在Service里面onBind(Intent intent)里面Intent就是bindService(Intent aIntent)中的aIntent对象。

二:生命周期

Stared Service
当调用startService()的完成后,该方法立即返回,然后Android System调用ServiceonCreate()方法(只在第一次的时候调用),然后再调用onStartCommand()方法,这时Service进入Active Lifetime。然后该Service无限的运行下去,直到Service内部调用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值