class MyService : Service() {
val mBinder=DownLoadBinder()
// 继承Binder,内部提供下载方法
class DownLoadBinder:Binder(){
fun startDownload(){
Log.e("MYService", "startDownload: ", )
}
fun getProgress(){
Log.e("MYService", "getProgress: ", )
}
}
override fun onBind(intent: Intent): IBinder {
return mBinder
}
// service创建时调用
override fun onCreate() {
Log.e("service", "onCreate: ")
super.onCreate()
}
// 在每次service启动时调用
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.e("service", "onStartCommand: ", )
return super.onStartCommand(intent, flags, startId)
}
// 在service销毁时调用
override fun onDestroy() {
Log.e("service", "onDestroy: ", )
super.onDestroy()
}
}
/**
* 在activity中和service绑定
* 1.创建类并继承自service
* 2.在manifests中进行注册
* 3.在service中定义类继承自binder
* 3.1. 重写onbind方法返回binder类
* 4、在activity中声明binder变量
* 4.1在activity中声明一个实现了ServiceConnection对象
* 4.1.1重写onServiceConnection方法 当activity和service绑定时调用
* 4.1.2重写onServiceDisconnected方法 当service崩溃了或被强杀时调用
* 5.启动,绑定service对象
* 5.1.1 启动service ,使用Context对象的startService(Intent)
* 5.1.2 关闭service stopSer(Intent) Intent(this:Context,MyService::class.java)
* 5.2.1 绑定service 使用Context对象bindService(Intent,Connection,flag)
* 5.2.2 解绑service unbindService(Connection)
*
* service生命周期 创建时调用onCreate
* 启动时调用onStartCommand
* 绑定时调用onBind
* 销毁时调用onDestroy
* 当service 启动并绑定 需关闭service并解绑才会 onDestroy
*/
class MainActivity : AppCompatActivity() {
lateinit var binding:ActivityMainBinding
// 声明binder变量
lateinit var downloadBinder:MyService.DownLoadBinder
// 创建一个匿名对象并实现ServiceConnection接口
val connection = object : ServiceConnection {
// service 绑定时调用
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
downloadBinder = service as MyService.DownLoadBinder
downloadBinder.startDownload()
downloadBinder.getProgress()
}
// service 崩溃或被强杀时调用
override fun onServiceDisconnected(name: ComponentName?) {
Log.e("activity", "onServiceDisconnected: ", )
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.btnStart.setOnClickListener {
val intent:Intent = Intent(this,MyService::class.java)
// 启动service
startService(intent)
}
binding.btnStop.setOnClickListener {
val intent=Intent(this,MyService::class.java)
// 停止service
stopService(intent)
}
binding.btnBind.setOnClickListener {
val intent=Intent(this,MyService::class.java)
// 绑定service
bindService(intent,connection, Context.BIND_AUTO_CREATE)
}
binding.btnUnbind.setOnClickListener {
// 解绑service
unbindService(connection)
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="56dp"
android:layout_marginTop="308dp"
android:text="启动service"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="256dp"
android:layout_marginTop="308dp"
android:text="关闭service"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_unbind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="256dp"
android:layout_marginTop="428dp"
android:text="unbind"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_bind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="72dp"
android:layout_marginTop="420dp"
android:text="bind"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>