赞
踩
服务是Android中的四大组件之一,它能够长期在后台运行且不提供用户界面。即使用户切到另一应用程序,服务仍可以在后台运行。
2.服务的创建
(1)创建Service子类
- class MyService : Service() {
-
- override fun onBind(intent: Intent): IBinder {
- TODO("Return the communication channel to the service.")
- }
- }
(2)在清单文件中配置
- <service
- android:name=".MyService"
- android:enabled="true"
- android:exported="true">
- </service>
3.服务的启动方式
(1)通过startService()方法启动
当通过startService()方法启动服务时,需要自身调用stopSelf()方法或者其他组件调用stopService()方法时服务才能停止。
(2)通过bindService()方法启动
当使用bingService()方法启动服务时,需要调用unbindService()方法解除绑定之后就会被销毁。
(3)即调用startService()方法,又调用了bingService()方法
这种情况下,要同时调用stopService()和unbindService()方法。
4.Service的生命周期
5.Activity和Service进行通信
Activity和Service之间的通信由IBinder负责,在Activity中,创建一个类实现ServiceConnection接口,并且在这个类中重写onServiceConnected方法(当Service被绑定时会回调这个方法)和onServiceDisconnected方法(Service的创建进程崩溃或者被杀掉才会调用),然后再绑定Service。
- class MainActivity : AppCompatActivity() {
- lateinit var myBinder:MyService.mBinder
- private val connection=object :ServiceConnection{
- override fun onServiceConnected(p0: ComponentName?, p1: IBinder?) {
- myBinder=p1 as MyService.mBinder
- myBinder.a()
- }
- override fun onServiceDisconnected(p0: ComponentName?) {
-
- }
- }
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- val bindbutton:Button=findViewById(R.id.bindbutton)
- val unbindbutton:Button=findViewById(R.id.unbindbutton)
- bindbutton.setOnClickListener {
- val intent=Intent(this,MyService::class.java)
- bindService(intent,connection,Context.BIND_AUTO_CREATE)//绑定Service
- }
- unbindbutton.setOnClickListener {
- unbindService(connection)//解绑Service
- }
- }
- }

在Service中,需要创建一个类继承Binder,在onBind()方法中返回这个类的实例。
- class MyService : Service() {
- private val myBinder=mBinder()
- class mBinder:Binder(){
- fun a(){
- Log.d("data","service")
- }
- }
- override fun onBind(intent: Intent): IBinder {
- return myBinder
- }
-
- override fun onCreate() {
- super.onCreate()
- Log.d("data","onCreate")
- }
- override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
- Log.d("data","onStartCommand")
- return super.onStartCommand(intent, flags, startId)
- }
- override fun onDestroy() {
- super.onDestroy()
- Log.d("data","onDestroy")
- }
- }

6.实现前台Service
前台服务执行一些用户能注意到的操作。
代码如下:
需要先进行权限声明
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
- class MyService : Service() {
- private val myBinder=mBinder()
- class mBinder:Binder(){
- fun a(){
- Log.d("data","service")
- }
- }
- override fun onBind(intent: Intent): IBinder {
- return myBinder
- }
-
- override fun onCreate() {
- super.onCreate()
- Log.d("data","onCreate")
- val manager=getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
- if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
- val channel=NotificationChannel("my_service","前台Service通知",NotificationManager.IMPORTANCE_DEFAULT)
- manager.createNotificationChannel(channel)
- }
- val intent=Intent(this,MainActivity::class.java)
- val pi=PendingIntent.getActivity(this,0,intent,0)
- val notification=NotificationCompat.Builder(this,"my_service")
- .setContentTitle("这是主题")
- .setContentText("这是内容")
- .setSmallIcon(R.drawable.ic_baseline_favorite_border_24)
- .build()
- startForeground(1,notification)
-
- }
- override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
- Log.d("data","onStartCommand")
- return super.onStartCommand(intent, flags, startId)
- }
- override fun onDestroy() {
- super.onDestroy()
- Log.d("data","onDestroy")
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。