本文共 4311 字,大约阅读时间需要 14 分钟。
package com.lyr.myapplication.serviceimport android.content.Intentimport android.os.Binderimport android.os.IBinderimport android.util.Logimport androidx.lifecycle.LifecycleServiceimport androidx.lifecycle.MutableLiveDataimport androidx.lifecycle.lifecycleScopeimport kotlinx.coroutines.delayimport kotlinx.coroutines.launchclass ExampleService : LifecycleService() { val number: MutableLiveData = MutableLiveData(0) override fun onCreate() { super.onCreate() Log.i("xxx", "开启服务") } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { lifecycleScope.launch{ while (true) { delay(1_000)// Log.d("xx","${++number}") } } return super.onStartCommand(intent, flags, startId) } inner class ExampleBinder : Binder() { val service:ExampleService = this@ExampleService } override fun onBind(intent: Intent): IBinder { super.onBind(intent) lifecycleScope.launch{ while (true) { delay(1_000) number.value = number.value?.plus(1) } } return ExampleBinder() }}
fragment 代码:
package com.lyr.myapplicationimport android.content.ComponentNameimport android.content.Contextimport android.content.Intentimport android.content.ServiceConnectionimport android.os.Bundleimport android.os.IBinderimport android.util.Logimport android.view.LayoutInflaterimport android.view.Viewimport android.view.ViewGroupimport androidx.fragment.app.Fragmentimport androidx.lifecycle.Observerimport com.lyr.myapplication.service.ExampleServiceimport kotlinx.android.synthetic.main.fragment_blank1.*// TODO: Rename parameter arguments, choose names that match// the fragment initialization parameters, e.g. ARG_ITEM_NUMBERprivate const val ARG_PARAM1 = "param1"private const val ARG_PARAM2 = "param2"/** * A simple [Fragment] subclass. * Use the [BlankFragment1.newInstance] factory method to * create an instance of this fragment. */class BlankFragment1 : Fragment() { // TODO: Rename and change types of parameters override fun onCreate(savedInstanceState: Bundle?) { Log.i("xx", "fragment create") super.onCreate(savedInstanceState)// Intent// activity?.startService(Intent(activity, ExampleService::class.java)) val intent = Intent(this.activity, ExampleService::class.java) val serviceConnection = object : ServiceConnection { val at = activity as ExampleActivity override fun onServiceDisconnected(name: ComponentName?) { } override fun onServiceConnected(name: ComponentName?, service: IBinder?) { (service as ExampleService.ExampleBinder).service .number.observe(at, Observer { f1_text.text = "$it" }) Log.i("xxoo", "绑定服务了OK") } } activity?.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE) } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_blank1, container, false) } companion object { /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment BlankFragment1. */ // TODO: Rename and change types and number of parameters @JvmStatic fun newInstance(param1: String, param2: String) = BlankFragment1().apply { arguments = Bundle().apply { putString(ARG_PARAM1, param1) putString(ARG_PARAM2, param2) } } }}
多次调用startService,该Service只能被创建一次,即该Service的onCreate方法只会被调用一次。但是每次调用startService,onStartCommand方法都会被调用。Service的onStart方法在API 5时被废弃,替代它的是onStartCommand方法。
第一次执行bindService时,onCreate和onBind方法会被调用,但是多次执行bindService时,onCreate和onBind方法并不会被多次调用,即并不会多次创建服务和绑定服务。
如果要和 服务通信,需要使用 bindservice 的方法
转载地址:http://qzyzi.baihongyu.com/