当前位置:   article > 正文

android开发,使用kotlin学习Service_kotlin service

kotlin service
  1. 服务简介

服务是Android中的四大组件之一,它能够长期在后台运行且不提供用户界面。即使用户切到另一应用程序,服务仍可以在后台运行。

   2.服务的创建

(1)创建Service子类

  1. class MyService : Service() {
  2. override fun onBind(intent: Intent): IBinder {
  3. TODO("Return the communication channel to the service.")
  4. }
  5. }

(2)在清单文件中配置

  1. <service
  2. android:name=".MyService"
  3. android:enabled="true"
  4. android:exported="true">
  5. </service>

3.服务的启动方式

(1)通过startService()方法启动

当通过startService()方法启动服务时,需要自身调用stopSelf()方法或者其他组件调用stopService()方法时服务才能停止。

(2)通过bindService()方法启动

当使用bingService()方法启动服务时,需要调用unbindService()方法解除绑定之后就会被销毁。

 (3)即调用startService()方法,又调用了bingService()方法

这种情况下,要同时调用stopService()和unbindService()方法。

4.Service的生命周期

  • onCreate():第一次创建服务时执行的方法。
  • onDestory():服务被销毁时执行的方法。
  • onStartCommand():访问者通过startService(intent)启动,服务时执行的方法。
  • onBind():使用bindService()方式启动服务调用的方法。
  • onUnbind():解除绑定时调用的方法。
  • 在这里插入图片描述

 

5.Activity和Service进行通信

Activity和Service之间的通信由IBinder负责,在Activity中,创建一个类实现ServiceConnection接口,并且在这个类中重写onServiceConnected方法(当Service被绑定时会回调这个方法)和onServiceDisconnected方法(Service的创建进程崩溃或者被杀掉才会调用),然后再绑定Service。

  1. class MainActivity : AppCompatActivity() {
  2. lateinit var myBinder:MyService.mBinder
  3. private val connection=object :ServiceConnection{
  4. override fun onServiceConnected(p0: ComponentName?, p1: IBinder?) {
  5. myBinder=p1 as MyService.mBinder
  6. myBinder.a()
  7. }
  8. override fun onServiceDisconnected(p0: ComponentName?) {
  9. }
  10. }
  11. override fun onCreate(savedInstanceState: Bundle?) {
  12. super.onCreate(savedInstanceState)
  13. setContentView(R.layout.activity_main)
  14. val bindbutton:Button=findViewById(R.id.bindbutton)
  15. val unbindbutton:Button=findViewById(R.id.unbindbutton)
  16. bindbutton.setOnClickListener {
  17. val intent=Intent(this,MyService::class.java)
  18. bindService(intent,connection,Context.BIND_AUTO_CREATE)//绑定Service
  19. }
  20. unbindbutton.setOnClickListener {
  21. unbindService(connection)//解绑Service
  22. }
  23. }
  24. }

在Service中,需要创建一个类继承Binder,在onBind()方法中返回这个类的实例。

  1. class MyService : Service() {
  2. private val myBinder=mBinder()
  3. class mBinder:Binder(){
  4. fun a(){
  5. Log.d("data","service")
  6. }
  7. }
  8. override fun onBind(intent: Intent): IBinder {
  9. return myBinder
  10. }
  11. override fun onCreate() {
  12. super.onCreate()
  13. Log.d("data","onCreate")
  14. }
  15. override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
  16. Log.d("data","onStartCommand")
  17. return super.onStartCommand(intent, flags, startId)
  18. }
  19. override fun onDestroy() {
  20. super.onDestroy()
  21. Log.d("data","onDestroy")
  22. }
  23. }

6.实现前台Service

前台服务执行一些用户能注意到的操作。

代码如下:

需要先进行权限声明

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
  1. class MyService : Service() {
  2. private val myBinder=mBinder()
  3. class mBinder:Binder(){
  4. fun a(){
  5. Log.d("data","service")
  6. }
  7. }
  8. override fun onBind(intent: Intent): IBinder {
  9. return myBinder
  10. }
  11. override fun onCreate() {
  12. super.onCreate()
  13. Log.d("data","onCreate")
  14. val manager=getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  15. if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
  16. val channel=NotificationChannel("my_service","前台Service通知",NotificationManager.IMPORTANCE_DEFAULT)
  17. manager.createNotificationChannel(channel)
  18. }
  19. val intent=Intent(this,MainActivity::class.java)
  20. val pi=PendingIntent.getActivity(this,0,intent,0)
  21. val notification=NotificationCompat.Builder(this,"my_service")
  22. .setContentTitle("这是主题")
  23. .setContentText("这是内容")
  24. .setSmallIcon(R.drawable.ic_baseline_favorite_border_24)
  25. .build()
  26. startForeground(1,notification)
  27. }
  28. override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
  29. Log.d("data","onStartCommand")
  30. return super.onStartCommand(intent, flags, startId)
  31. }
  32. override fun onDestroy() {
  33. super.onDestroy()
  34. Log.d("data","onDestroy")
  35. }
  36. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/252731
推荐阅读
相关标签
  

闽ICP备14008679号