【兼容一切Android-API level版本】启动Service的方法总结

该文章展示了一个使用Kotlin编写的AndroidService示例,服务创建了一个内部Binder对象用于数据交互,并通过Notification保持前台运行。在Service生命周期的各个阶段如onCreate、onStartCommand、onBind和onDestroy中进行了相应操作。同时,文章还涉及了针对AndroidAPILevel26及以上版本的启动Service的方法,以及如何检查Service的状态和停止Service。
摘要由CSDN通过智能技术生成

TestService.kt

package com.example.kotlingradletemplatefirst.service

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.Build
import android.os.IBinder
import android.util.Log
import com.example.kotlingradletemplatefirst.R

class TestService : Service() {

    companion object {
        // 记录当前Service是否启动
        var isRunning = false
    }

    private var count: Int = 0
    private var quit = false
    private var binder = MyBinder()

    inner class MyBinder : Binder() {
        fun getCount(): Int {
            return this@TestService.count
        }
    }

    override fun onBind(p0: Intent?): IBinder {
        """
            功能:实现接口方法
        """.trimIndent()
        Log.e("TestService", "onBind")
        return binder
    }

    override fun onUnbind(intent: Intent?): Boolean {
        Log.e("TestService", "onUnbind")
        return super.onUnbind(intent)
    }

    override fun onCreate() {
        super.onCreate()
        this.supportAfterLevel26API()
        Log.e("TestService", "onCreate")
        object : Thread() {
            override fun run() {
                while (!quit) {
                    println("定时器执行中...")
                    try {
                        sleep(1000)
                    } catch (e: InterruptedException) {
                        e.printStackTrace()
                    }
                    this@TestService.count++
                }
            }
        }.start()

        startForeground(1, Notification())
    }

    private fun supportAfterLevel26API() {
        /**
         * 功能:支持【Level26API以上版本】启动Service方法!
         */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            /**
             * 1、【Level26API以上版本】的启动Service方法!
             */
            val notificationChannel = NotificationChannel(
                "my_channel_id",
                "My Channel",
                NotificationManager.IMPORTANCE_DEFAULT
            )
            val notificationManager = getSystemService(NotificationManager::class.java)
            notificationManager.createNotificationChannel(notificationChannel)
            val notification = Notification.Builder(this, "my_channel_id")
                .setContentTitle("My Service")
                .setContentText("Running...")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .build()
            // 基于Service的onCreate时,必须调用!
            startForeground(1, notification)
        }
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        Log.e("TestService", "onStartCommand")
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e("TestService", "onDestroy")
        // 销毁时必须退出while循环!
        this.quit = true
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            /**
             * 1、【Level26API以上版本】的启动Service方法!
             */
            stopForeground(Service.STOP_FOREGROUND_REMOVE)
        } else {
            stopForeground(true)
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/start_service"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:backgroundTint="#00f"
        android:text="startService"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/get_binder_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:backgroundTint="#0f0"
        android:text="getBinderData"
        android:textAllCaps="false" />


    <Button
        android:id="@+id/check_service_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:backgroundTint="#f0f"
        android:text="checkServiceStatus"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/stop_service"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:backgroundTint="#f00"
        android:text="stopService"
        android:textAllCaps="false" />
</LinearLayout>

MainActivity.kt

package com.example.kotlingradletemplatefirst


import android.app.ActivityManager
import android.app.NotificationManager
import android.app.Service
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Build
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.example.kotlingradletemplatefirst.service.TestService
import java.util.logging.Handler


class MainActivity : AppCompatActivity() {

    private var binder: TestService.MyBinder? = null
    private var conn: ServiceConnection? = null
    private var componentName: ComponentName? = null
    private var isBinder: Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val intent = Intent(this, TestService::class.java)

        println("TestService::class.java=${TestService::class.java.name}")
        this.conn = object : ServiceConnection {
            override fun onServiceConnected(p0: ComponentName?, p1: IBinder?) {
                Log.e("MainActivity", "onServiceConnected")
                /**
                 * 1、连接成功,会返回绑定的binder对象!
                 */
                this@MainActivity.binder = (p1 as TestService.MyBinder)
            }

            override fun onServiceDisconnected(p0: ComponentName?) {
                Log.e("MainActivity", "onServiceDisconnected")
            }
        }

        findViewById<Button>(R.id.start_service).setOnClickListener {
            /**
             * 1、启动或绑定Service
             * 2、如果是startService,则必须手动调用stopService
             * 3、如果是bindService,则必须手动调用unbindService
             */
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                /**
                 * 1、【Level26API以上版本】的启动Service方法!
                 */
                this.componentName = startForegroundService(intent)
                Log.e(
                    "MainActivity",
                    "当前SDK版本:${Build.VERSION.SDK_INT},正在执行startForegroundService启动..."
                )
            } else {
                this.componentName = startService(intent)
                // this.isBinder = bindService(intent, this.conn!!, Service.BIND_AUTO_CREATE)
            }
            // 通过TestService的静态属性记录启动状态!
            TestService.isRunning = true
        }

        findViewById<Button>(R.id.stop_service).setOnClickListener {
            /**
             * 1、停止或解绑服务!
             */
            if (this.componentName != null) {
                stopService(intent)
                this.componentName = null
            }

//            if (this.isBinder) {
//                unbindService(this.conn!!)
//                this.isBinder = false
//            }

            TestService.isRunning = false
        }

        findViewById<Button>(R.id.check_service_status).setOnClickListener {
            /**
             * 功能:自动检测Service是状态【是否已启动】
             * 特殊说明:
             *      1、getRunningServices此方法已被弃用警告【原因是:消耗内存电量】
             *      2、基于【1】的原因,建议在TestService增加一个静态属性isRunning代替此方法!
             */
            val manager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            val runningServices = manager.getRunningServices(Integer.MAX_VALUE)
            var num = 0
            for (service in runningServices) {
                if (TestService::class.java.name == service.service.className) {
                    // MyService 已经启动
                    Log.e("MainActivity", "service=${TestService::class.java.name} is Starting...")
                    num++
                }
            }
            Log.e("MainActivity", "service=${TestService::class.java.name}的启动的个数为【$num】")
        }


        findViewById<Button>(R.id.get_binder_data).setOnClickListener {
            /**
             * 1、获取Service绑定的数据
             */
            Log.e("MainActivity", "count=${this.binder?.getCount()}")
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值