Service

Service

Android四大组件之一,后台运行

创建,配置Service

1.定义一个继承service的子类
2.在AndroidManifest.xml文件中配置该Service
//如果是在AndroidStudio中,可以直接new一个Service,快速构建.
1.创建:


import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.util.Log

class MyService : Service() {
    /*
        必须实现的方法.绑定IBinder对象,应用程序通过该对象与Service通信
     */
    override fun onBind(intent: Intent): IBinder? {
        throw UnsupportedOperationException("Not yet implemented")
        Log.d("myService","onBind")
    }

    /*
    * 在Service第一次被创建后立即回调该方法.
    * */
    override fun onCreate() {
        super.onCreate()
        Log.d("myService","onCreate")

    }

    /*
    * 每次调用StartService(intent)方法是启动该Serviece并回调.
    * */
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        return super.onStartCommand(intent, flags, startId)
        Log.d("myService","onStartCommand")
    }

    /*
    * 在Service被关闭之前回调该方法
    * */
    override fun onDestroy() {
        super.onDestroy()
        Log.d("myService","onDestroy")
    }
}

2.配置:

<service android:name=".MyService"></service>

运行Service

1.通过Context.startService()方法.(与访问者没有关联,访问者退出了,Service然在运行)
2.通过Context.bindService()方法.(访问者与service关联,访问者一旦退出,Service也就终止)
1.start

 startService(intent)//启动service
 startService(intent)//启动service
 stopService(intent)//停止service
01-12 01:11:49.001 2737-2737/? D/myService: onCreate
01-12 01:11:49.001 2737-2737/? D/myService: onStartCommand
01-12 01:11:49.001 2737-2737/? D/myService: onStartCommand
01-12 01:11:49.001 2737-2737/? D/myService: onDestroy

2.bind:

  bindService(intent,connection,Service.BIND_AUTO_CREATE)
        //1.intent: Service 开启的service 2.connection: ServiceConnetion 监听访问者和Service 3.flag:Boolean 是否自动创建Service 0 则不自动创建,BIND_AUTO_CREATE自动创建

Activvity里:

    var binder: MyService.MyBinder? = null
    private val connection: ServiceConnection = object : ServiceConnection{
        /**
         * 当Activity与Service断开连接时回调该方法
         */
        override fun onServiceDisconnected(name: ComponentName?) {
            println("--disconnected--")

        }
        /**
         * 当Activity与Service连接成功时回调该方法
         */
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            println("--connected--")
            binder = service as MyService.MyBinder? //获取service的onBind方法所返回的MyBinder对象
        }
    }

service里:

    private var count: Int = 0
    private val binder = MyBinder()

    inner class MyBinder : Binder() {
        fun getCount(): Int {
            return count
        }
    }
    /*
        必须实现的方法.绑定IBinder对象,应用程序通过该对象与Service通信
     */
    override fun onBind(intent: Intent): IBinder? {
        Log.d("myService", "onBind")
        return binder //返回binder对象
    }
private var quit: Boolean = false 
/* * 在Service第一次被创建后立即回调该方法. * */ 
override fun onCreate() {
    super.onCreate()
    Log.d("myService", "onCreate")
    thread {
  while (!quit) {
            try {
                Thread.sleep(1000)
            } catch (e: Exception) {

            }finally {
                count++
            }
        }
    }

然后设置一个按钮获取service里的count值:

getState.setOnClickListener { toast("service的count值:${binder?.getCount()}") }

这里写图片描述
这里写图片描述

当然别忘了解绑:

        unbindService(connection)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值