Android Service学习

Service的生命周期:

这里写图片描述

Android官方文档:

http://www.android-doc.com/reference/android/app/Service.html#ServiceLifecycle
Service Lifecycle

There are two reasons that a service can be run by the system. If someone calls Context.startService() then the system will retrieve the service (creating it and calling its onCreate() method if needed) and then call its onStartCommand(Intent, int, int) method with the arguments supplied by the client. The service will at this point continue running until Context.stopService() or stopSelf() is called. Note that multiple calls to Context.startService() do not nest (though they do result in multiple corresponding calls to onStartCommand()), so no matter how many times it is started a service will be stopped once Context.stopService() or stopSelf() is called; however, services can use their stopSelf(int) method to ensure the service is not stopped until started intents have been processed.

For started services, there are two additional major modes of operation they can decide to run in, depending on the value they return from onStartCommand(): START_STICKY is used for services that are explicitly started and stopped as needed, while START_NOT_STICKY or START_REDELIVER_INTENT are used for services that should only remain running while processing any commands sent to them. See the linked documentation for more detail on the semantics.

Clients can also use Context.bindService() to obtain a persistent connection to a service. This likewise creates the service if it is not already running (calling onCreate() while doing so), but does not call onStartCommand(). The client will receive the IBinder object that the service returns from its onBind(Intent) method, allowing the client to then make calls back to the service. The service will remain running as long as the connection is established (whether or not the client retains a reference on the service’s IBinder). Usually the IBinder returned is for a complex interface that has been written in aidl.

A service can be both started and have connections bound to it. In such a case, the system will keep the service running as long as either it is started or there are one or more connections to it with the Context.BIND_AUTO_CREATE flag. Once neither of these situations hold, the service’s onDestroy() method is called and the service is effectively terminated. All cleanup (stopping threads, unregistering receivers) should be complete upon returning from onDestroy().

简单翻译下:

系统可以通过两种方式来启动一个Service:

  1. 如果使用了Context.startService(),系统会获取一个Service(如果是第一次调用的话,会调用onCreate()方法),然后会调用onStartCommand(Intent, int, int)方法。这时候,该Service会一直运行下去,直到调用了Context.stopService()或者Service自身调用了stopSelf()。如果多次调用Context.startService(),只会在第一次调用onCreate(),之后都只会调用onStartCommand()。并且,不论调用了多少次startService,如果调用一次stopService或者stopSelf(),该Service也会被销毁

  2. 用户可以调用Context.bindService()来将该Service和另一个绑定。(例如将一个Activity和一个Service绑定,这样的话,当Activity销毁的时候,该Service也会销毁)。这时候会调用onCreate()->onBind(),但是不会调用onStartCommand(),调用者会在connection的回调函数中获取一个IBinder对象,就是该Service的onBinde()中返回的对象。同样,如果该Service已经调用过onCeate()则不会再调用,直接调用onBind()。当调用者onDestroy()或者调用了unbindService()方法,都会调用onUnBind()->onDestroy()。

一个Service既可以被startService,又可以被bindService。除非stopService(或者stopSelf)和unbindService()同时调用,该Service才会onDestroy()。

绑定Service注意事项:

应用组件(客户端)可以通过 bindService()方法绑定到service,Android系统随后会调用service的 onBind()方法,返回一个 IBinder 用于和service交互。

  绑定过程是异步的(asynchronous) , bindService()方法会立即返回,但是不会返回IBinder对象给客户端。

  为了接收到IBinder,客户端必须创建一个ServiceConnection的实例,并且把它传给 bindService(),这个 ServiceConnection中会包含一个回调函数,系统调用它来传递 IBinder对象。

  注意:仅仅是activity,service和content provider可以和service绑定,你不能把一个broadcast receiver和一个service绑定。

为了将客户端和service绑定,你必须:

1.实现ServiceConnection

  其中需要实现两个回调方法:

  onServiceConnected()

  系统调用这个方法来传递service的onBind()方法返回的 IBinder 。

  onServiceDisconnected()

  当和service的连接意外丢失时,系统会调用这个方法。如果是客户端解除绑定,系统不会调用这个方法。

2.调用 bindService(),并传递 ServiceConnection的实现。

3.当系统调用你的 onServiceConnected(),你可以开始用接口定义的方法访问service。

4.要断开连接,调用 unbindService()。

  当你的客户端被销毁的时候,会断开与service的连接,但是你应该在停止与service的交互时断开连接或者当activity暂停时断开连接,这样service就可以在它不被使用的时候关闭。

另外,如果你的service被开启并且接受绑定,那么当系统调用你的 onUnbind()方法时,如果你想要在下次客户端绑定的时候接受一个onRebind()的调用(而不是调用 onBind()),你可以选择在 onUnbind()中返回true。

  onRebind()的返回值为void,但是客户端仍然在它的 onServiceConnected()回调方法中得到 IBinder 对象。

  下图展示了这种service(被开启,还允许绑定)的生命周期:
  这里写图片描述

测试代码位置:
https://github.com/YoungBear/ServiceLearn

参考:
http://www.cnblogs.com/mengdd/archive/2013/03/24/2979944.html

http://www.cnblogs.com/mengdd/archive/2013/03/24/2979903.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值