Service服务是Android四大组件之一,是Android提供的一种的 不需要和用户交互,且需要长期运行任务的解决方案。

    Service是 Android中实现程序后台运行的解决方案,它非常适合执行那些不需要和用户交互 而且还要求长期运行的任务。 Service的运行不依赖于任何用户界面,即使程序被切换到后台, 或者用户打开了另外一个应用程序, Service仍然能够保持正常运行。

    image.png

    1. 继承自Service类的。
    2. onBind()唯一抽象方法。 ```kotlin import android.app.Service import android.content.Intent import android.os.IBinder

    class FloatLogo : Service() { override fun onBind(p0: Intent?): IBinder? { TODO(“Not yet implemented”) } }

    1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/326318/1629628860570-27a31b34-d733-42f9-bcf2-b616ff9982aa.png#clientId=u2379e6fd-eea0-4&from=paste&height=591&id=u2abc625a&margin=%5Bobject%20Object%5D&name=image.png&originHeight=591&originWidth=1211&originalType=binary&ratio=1&size=222592&status=done&style=none&taskId=u554ed8df-a9df-4134-9912-240fcdb2745&width=1211)
    2. ```kotlin
    3. class FloatLogo : Service() {
    4. private val TAG = "FloatLogo"
    5. override fun onCreate() {
    6. Log.i(TAG, "onCreate: ")
    7. super.onCreate()
    8. }
    9. override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    10. Log.i(TAG, "onStartCommand: ")
    11. return super.onStartCommand(intent, flags, startId)
    12. }
    13. override fun onBind(p0: Intent?): IBinder? {
    14. Log.i(TAG, "onBind: ")
    15. TODO("Not yet implemented")
    16. }
    17. }