博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
fragment中绑定服务
阅读量:3953 次
发布时间:2019-05-24

本文共 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/

你可能感兴趣的文章
Android电源管理
查看>>
Android电源管理机制分析(zz)
查看>>
kobject与sysfs
查看>>
sysfs 文件系统
查看>>
Linux 内核/sys 文件系统之sysfs 属性文件
查看>>
Google_android_JNI使用方法
查看>>
Android JNI
查看>>
ARM与嵌入式linux如何入门
查看>>
git命令入门
查看>>
PreferenceActivity 全接触
查看>>
Android之PreferenceActivity
查看>>
在Ubuntu上搭建嵌入式Linux开发环境
查看>>
C语言数据类型:联合(union)
查看>>
记录每次更新到仓库
查看>>
PXA270嵌入式系统设计一:电源管理部分收藏
查看>>
PXA270嵌入式系统设计(2)—时钟及复位部分
查看>>
linux 多点触控协议
查看>>
中断服务下半部之工作队列详解
查看>>
怎样做一块好的pcb板
查看>>
内核中的 likely() 与 unlikely()
查看>>