Android-Service的基本用法

Service 的基本用法


开始学习四大组件之一的Service

定义一个Service

定义一个类继承Service

class MyService : Service() {  
    override fun onCreate() {//创建时调用  
        super.onCreate()  
    }  
  
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {//启动时调用  
        return super.onStartCommand(intent, flags, startId)  
    }  
  
    override fun onDestroy() {//销毁调用  
        super.onDestroy()  
    }  
    override fun onBind(intent: Intent): IBinder {  
        TODO("Return the communication channel to the service.")  
    }  
}

重写一些常用方法

启动和停止Service

启动和停止Service非常简单

binding.startServiceBtn.setOnClickListener {  
    val intent = Intent(this,MyService::class.java)  
    startService(intent)  
}  
binding.stopServiceBtn.setOnClickListener {  
    val intent = Intent(this,MyService::class.java)  
    stopService(intent)  
}

只需要定义一个Intent然后调用startService/stopService方法即可。

Activity和Service进行通信

调用onBind可以进行通信。
现在我们要实现一个功能;在MyService提供一个下载功能,在Activity中指挥Sevice开始下载,以及随时查看下载进度。

private val mBinder = DownloadBinder()  
class DownloadBinder : Binder(){  
    fun startDownload(){  
        Log.d("MyService","startDownload executed")  
    }  
    fun getProgress():Int{  
        Log.d("MyService","getProgress executed")  
        return 0  
    }  
}
override fun onBind(intent: Intent): IBinder {  
    return mBinder  
}

首先新建了一个DownloadBinder类继承自Binder,类里简单实现两个方法,一个是开始下载,另一个是查看进度,然后再onBind中返回这个类
在MainActivity中修改代码

  
class MainActivity : AppCompatActivity() {  
    lateinit var downloadBinder: MyService.DownloadBinder  
    private val connection = object : ServiceConnection{  
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {  
            downloadBinder = service as MyService.DownloadBinder  
            downloadBinder.startDownload()  
            downloadBinder.getProgress()  
        }  
  
        override fun onServiceDisconnected(name: ComponentName?) {  
            TODO("Not yet implemented")  
        }  
    }  
    override fun onCreate(savedInstanceState: Bundle?) {  
        super.onCreate(savedInstanceState)  
        setContentView(R.layout.activity_main)  
        val binding = ActivityMainBinding.inflate(layoutInflater)  
        setContentView(binding.root)  
       ...
        binding.bindServiceBtn.setOnClickListener {  
            val intent = Intent(this,MyService::class.java)  
            bindService(intent,connection,Context.BIND_AUTO_CREATE)  
        }  
        binding.unbindServiceBtn.setOnClickListener {  
            unbindService(connection)  
        }  
    }  
  
}

这里首先创建了一个ServiceConnection的匿名类,并且在里面重写了onServiceConnected()方法和onServiceDisconnected()方法。
onServiceConnected()方法会在Activity与Service绑定成功时调用,onServiceDisconnected()则会在绑定接触时调用(因为一般用不到所以不写具体内容)。
那么在onServiceConnected()中,我们先获得一个DownloadBinder的实例,然后在调用方法。
点击事件的实现则更加简单
建立绑定依旧是使用Intent,调用bindService接收三个参数,第一个是Intent对象,第二个是前面创建出的ServiceConnection实例,第三个是flag,这里的BIND_AUTO_CREATE是指绑定后自动创建Service。(这会使得onStartCommand不会执行)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值