启动和停止
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
startServiceBtn.setOnClickListener {
val intent = Intent(this, MyService::class.java)
startService(intent) // Service
}
stopServiceBtn.setOnClickListener {
val intent = Intent(this, MyService::class.java)
stopService(intent) // Service
}
}
}
类重写的函数
class MyService : Service() {
override fun onCreate() {
super.onCreate() // 启动时调用
}
override fun onDestroy() {
super.onDestroy() // 销毁时调用
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
return super.onStartCommand(intent, flags, startId) // 调用service时调用
}
override fun onBind(intent: Intent): IBinder {
// 其他组件与service建立连接会调用
}
}
与Activity通信
MyService.kt
class MyService : Service() {
private val mBinder = mybinder()
class mybinder: Binder(){
fun way(){
println("way")
}
}
override fun onCreate() {
super.onCreate() // 启动时调用
}
override fun onDestroy() {
super.onDestroy() // 销毁时调用
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
return super.onStartCommand(intent, flags, startId) // 调用service时调用
}
override fun onBind(intent: Intent): IBinder {
return mBinder
}
}
MainActivity.kt
class MainActivity : AppCompatActivity() {
lateinit var mybinder: MyService.mybinder
private val connection = object :ServiceConnection{
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
mybinder = service as MyService.mybinder
mybinder.way()
// activity与service成功绑定时调用
}
override fun onServiceDisconnected(name: ComponentName?) {
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
val btn1 : Button = findViewById(R.id.btn1)
btn1.setOnClickListener {
val intent = Intent(this,MyService::class.java)
bindService(intent,connection,Context.BIND_AUTO_CREATE)
// 通过intent与service绑定
}
val btn2:Button = findViewById(R.id.btn2)
btn2.setOnClickListener {
unbindService(connection)
}
}
}
前台Service
修改myService的onCreate方法:
override fun onCreate() {
super.onCreate() // 启动时调用
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channel = NotificationChannel("myservice","前台",NotificationManager.IMPORTANCE_DEFAULT)
manager.createNotificationChannel(channel)
val intent = Intent(this,MainActivity::class.java)
val pi = PendingIntent.getActivity(this,0,intent, PendingIntent.FLAG_IMMUTABLE)
val notification = NotificationCompat.Builder(this,"myservice")
.setContentTitle("This is content title")
.setContentText("This is content text")
.setContentIntent(pi)
.build()
startForeground(1, notification)
}
IntentService
class MyIntentService : IntentService("MyIntentService") {
override fun onHandleIntent(intent: Intent?) {
// 子线程逻辑
Log.d("MyIntentService", "Thread id is ${Thread.currentThread().name}")
}
override fun onDestroy() {
super.onDestroy()
Log.d("MyIntentService", "onDestroy executed")
}
}
Service
可以与应用程序的其他组件(如Activity
)进行通信,而IntentService
通常用于执行与应用程序交互不直接相关的任务。Service
需要手动管理线程和生命周期,而IntentService
在单独的工作线程中运行,无需手动管理线程。