安卓开发安卓开发Android开发经验谈

安卓学习入门之Service基本使用方法

2019-04-09  本文已影响13人  蓝不蓝编程

简要介绍

Service是安卓的四大组件之一,常用来在后台执行一些耗时长的任务. Service的启动分为startService和bindService两种方式. 对于bindService方式,采用的类似服务端和客户端通信的方式,可以允许多个客户端连接到service上,允许对一个service断开连接,再次连接;activity可以不断调用service里的方法实现和service的通信(如在一个下载的service里,查询下载进度). 关于IntentService可以参考《安卓IntentService基本使用方法

Service生命周期

使用方法

  1. bindService方法
class SampleService : Service() {
    private val tag = SampleService::class.java.simpleName

    inner class MyBinder : Binder() {

        val service: SampleService
            get() = this@SampleService
    }

    private val binder = MyBinder()

    private fun doSomething() {
        log(tag, "doSomething")
        Thread.sleep(2000)
    }

    override fun onBind(intent: Intent?): IBinder? {
        log(tag, "onBind")
        doSomething()
        return binder
    }

    override fun onUnbind(intent: Intent?): Boolean {
        log(tag, "onUnbind")
        return false
    }


    override fun onDestroy() {
        log(tag, "onDestroy")
        super.onDestroy()
    }

    fun getRandomNumber(): Int {
        return Random.nextInt()
    }

}
class MainActivity : AppCompatActivity() {
    private val tag = MainActivity::class.java.simpleName
    private lateinit var service: SampleService
    private var isBound = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initListeners()
    }

    private fun initListeners() {
        bindBtn.setOnClickListener {
            val intent = Intent(this, SampleService::class.java)
            bindService(intent, serviceConnection, BIND_AUTO_CREATE)
        }
        unBindBtn.setOnClickListener { unbindService(serviceConnection) }
    }

    private val serviceConnection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName, binder: IBinder) {
            log(tag, "onServiceConnected")
            isBound = true
            val myBinder = binder as SampleService.MyBinder
            service = myBinder.service

            val num = service.getRandomNumber()
            log(tag, "getRandomNumber called,result:$num")
        }

        override fun onServiceDisconnected(name: ComponentName) {
            isBound = false
            log(tag, "onServiceDisconnected")
        }
    }
}
  1. startService方法
class SampleService : Service() {
    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    private val tag = SampleService::class.java.simpleName

    private fun doSomething() {
        log(tag, "doSomething")
        Thread.sleep(2000)
    }

    override fun onCreate() {
        log(tag, "onCreate")
        super.onCreate()
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        log(tag, "onStartCommand")
        thread(name = "worker-thread") {
            doSomething()
        }
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onDestroy() {
        log(tag, "onDestroy")
        super.onDestroy()
    }
}
class MainActivity : AppCompatActivity() {

    lateinit var serviceIntent: Intent
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        serviceIntent = Intent(this, SampleService::class.java)
        startService(serviceIntent)
    }

    override fun onDestroy() {
        super.onDestroy()
        stopService(serviceIntent)
    }

}

Demo源代码

https://github.com/cxyzy1/androidServiceDemo

安卓开发技术分享: https://www.jianshu.com/p/442339952f26
点击关注专辑,查看最新技术分享
更多技术总结好文,请关注:「程序园中猿」

上一篇下一篇

猜你喜欢

热点阅读