Learn Android Service

The first thing to note is that this article is for learning, not the original author, the source address [Android Service is completely resolved, everything you need to know about the service (Part 1) ( http://blog.csdn.net/guolin_blog/article/details/ 11952435/ )]

1. Basic usage of Service

2. Interaction between Service and Activity

3、Service 和 Thread

4. Remote Service

5. Cross-process communication

[To be improved. . .

〈1〉Service

Let's first look at the life cycle of Service
Service life cycle diagram

From the life cycle, you can clearly understand the Service, but let's write a Service:

class FirstService : Service() {
    override fun onBind(p0: Intent?): IBinder {
        // TODO
    }
}

The above is the most basic Service code (pseudo code), I guess onBind() may be the way to write code logic. We continue:

class FirstService : Service() {

    private val TAG = "WYF_TAG"

    override fun onBind(p0: Intent?): IBinder {
        // TODO
    }

    override fun onCreate() {
        super.onCreate()
        Log.i(TAG, "onCreate() executed")
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        Log.i(TAG, "onStartCommand() executed")
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.i(TAG, "onDestory() executed")
    }
}

This code can help us know the startup process of the Service very clearly, and it can be regarded as an acquaintance with the Service here.

<2> Interaction between Service and Activity

Service can help us deal with some time-consuming operations (friendly interaction), so how do we use Activity to operate Service?

Before writing this case, I think it is necessary to say some methods:

            /** 开启Service */
            R.id.mainStartService -> {
                intent = Intent(this, MyService::class.java)
                startService(intent)
            }
            /** 停止Service */
            R.id.mainStopService -> {
                intent = Intent(this, MyService::class.java)
                stopService(intent)
            }
            /** Bind Service */
            R.id.mainBindService -> {
                intent = Intent(this, MyService::class.java)
                bindService(intent, connection, Context.BIND_AUTO_CREATE)
            }
            /** Unbind Service */
            R.id.mainUnbindService -> {
                unbindService(connection)
            }

From the above part of the code, we can know some ways to operate the Service in the Activity, so how to make the Activity and the Service really communicate? See the following code snippet

This is an inner class of MyService that implements the download task:

class MyBinder : Binder() {
        fun startDownload() {
            Log.i("TAG", "startDownload() executed")

            Thread(Runnable {
                kotlin.run {
                    // 执行具体的下载任务
                }
            }).start()
        }
    }

We use it in MyService and return it:

    private val mBinder = MyBinder()

    override fun onBind(intent: Intent?): IBinder {
        return mBinder
    }

Call in Activity:

    lateinit var myBinder : MyService.MyBinder

    private val connection = object : ServiceConnection {
        override fun onServiceDisconnected(p0: ComponentName?) {

        }

        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            myBinder = service as MyService.MyBinder
            myBinder.startDownload()

        }

    }

<3> Service and Thread

In the blog post, the relationship between Service and Thread is mentioned. Here is a brief summary: Service and Thread have nothing to do with each other.

My understanding: Service (background) is a way to achieve friendly interaction, and Thread is to open multiple threads to work together to complete one or more tasks.

At this point, today's Service is over, so you will ask, what about remote Service and cross-process communication?

Let me explain here. The remote service blog post also said that it is not recommended, so I will not say it. As for cross-process communication and some other things not mentioned, they will be supplemented and improved gradually later.

Source code: https://github.com/fengwenyi/ServiceTest

About me
WenyiFeng(xfsy2014@gmail.com)

Copyright © 2017 fengwenyi.com. All Rights Reserved.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325442053&siteId=291194637