Service生命周期

https://www.cnblogs.com/huihuizhang/p/7623760.html

(1)Service是单例的,只要没有destroy,多次startService或bindService,onCreate都只有回调一次,对应的onStartCommand或onBind每次都会回调

(2)startService与stopService/stopSelf对应,bindService与unbindService/绑定方死亡(如Activity)对应,如果一个Service既被startService了,也被bindService,必须有对应的结束函数调用或绑定方死亡(不管先后顺序,都被调了就可以),Service才会结束,执行onDestroy

(3)当在旋转手机屏幕的时候,当手机屏幕在“横”“竖”变换时,此时如果你的 Activity 如果会自动旋转的话,旋转其实是 Activity 的重新创建,因此旋转之前的使用 bindService 建立的连接便会断开(Context 不存在了),对应服务也会结束;可能需要重新绑定

(4)onDestroy里你应当做一些清除工作,如停止在Service中创建并运行的线程。

bindService用于AIDL的示例学习

原文地址:https://www.cnblogs.com/genggeng/p/10080394.html