Service

启动和停止

 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在单独的工作线程中运行,无需手动管理线程。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值