ForegroundService

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.IBinder
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat


open class ForegroundService:Service(){

    override fun onBind(p0: Intent?): IBinder? {
        return null
    }

    override fun onCreate() {
        super.onCreate()
        startForeground()
    }

    open fun configNotification(builder:NotificationCompat.Builder):NotificationCompat.Builder{
        return  builder.setOngoing(true)
            .setSmallIcon(0)
            .setPriority(NotificationCompat.PRIORITY_MIN)
            .setCategory(Notification.CATEGORY_SERVICE)
    }


    @RequiresApi(Build.VERSION_CODES.O)
    open fun configNotificationChannel(chan:NotificationChannel):NotificationChannel{
        chan.description = "Channel description"
        chan.enableLights(true)
        chan.lightColor = Color.BLUE
        chan.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
        chan.vibrationPattern = longArrayOf(0L, 1000L, 500L, 1000L)
        chan.enableVibration(true)
        return chan
    }



    private fun startForeground() {
        var channelId: String? = null
        // 8.0 以上需要特殊处理
        channelId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel("zlgspace_channel_id", "ZLGForegroundService")
        } else {
            ""
        }
        var builder = NotificationCompat.Builder(this, channelId)
        builder = configNotification(builder)
        var notification = builder.build()
        startForeground(1, notification)
    }


    //创建通知通道
    @RequiresApi(Build.VERSION_CODES.O)
    private fun createNotificationChannel(channelId: String, channelName: String): String {
        var chan = NotificationChannel(
            channelId,
            channelName, NotificationManager.IMPORTANCE_NONE
        )
        chan = configNotificationChannel(chan)
        val service = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        service.createNotificationChannel(chan)
        return channelId
    }

}
### 概念 Android Foreground Service 即前台服务,是一种具有较高系统优先级、不易被系统回收的服务。与普通服务不同的是,前台服务会在系统的状态栏一直显示一个正在运行的图标,下拉状态栏后能看到更详细的信息,类似通知效果 [^3]。 ### 使用方法 #### 声明服务与获取权限 需要在 AndroidManifest.xml 中声明服务,并添加前台服务的权限: ```xml <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.app"> <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <service android:name=".AppService" android:enabled="true" android:exported="false"/> </application> </manifest> ``` #### Notification 初始化 在服务启动时,需要创建一个通知并调用 `startForeground` 方法将服务置于前台。从 Android 8.0(API 级别 26)开始,还需要创建通知渠道: ```java import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; import android.os.Build; import android.os.IBinder; import androidx.core.app.NotificationCompat; public class AppService extends Service { private static final String CHANNEL_ID = "ForegroundServiceChannel"; @Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { createNotificationChannel(); Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("Foreground Service") .setContentText("Running...") .setSmallIcon(R.drawable.ic_notification) .setContentIntent(pendingIntent) .build(); startForeground(1, notification); return START_STICKY; } @Override public IBinder onBind(Intent intent) { return null; } private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel serviceChannel = new NotificationChannel( CHANNEL_ID, "Foreground Service Channel", NotificationManager.IMPORTANCE_DEFAULT ); NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(serviceChannel); } } } ``` #### 启动服务 在 Activity 中启动前台服务: ```java import android.content.Intent; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent serviceIntent = new Intent(this, AppService.class); startService(serviceIntent); } } ``` ### 实现原理 Android 系统有一个 LowMemoryKiller 机制,会在系统内存不足时,根据服务的优先级来决定回收哪些进程。前台服务由于其系统优先级较高,被 LowMemoryKiller 回收的可能性较小。当调用 `startForeground` 方法时,系统会将该服务标记为前台服务,并在状态栏显示通知,以此提高服务的优先级 [^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值