package expo.modules.gaodemap.map.services import android.annotation.SuppressLint import android.app.Notification import android.app.NotificationChannel import android.app.NotificationManager import android.app.Service import android.content.Context import android.content.Intent import android.os.Build import android.os.IBinder import androidx.core.app.NotificationCompat /** * 定位前台服务 * 用于支持 Android 后台定位 */ class LocationForegroundService : Service() { companion object { private const val NOTIFICATION_ID = 1001 private const val CHANNEL_ID = "location_service_channel" private const val CHANNEL_NAME = "定位服务" fun start(context: Context) { val intent = Intent(context, LocationForegroundService::class.java) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(intent) } else { context.startService(intent) } } fun stop(context: Context) { val intent = Intent(context, LocationForegroundService::class.java) context.stopService(intent) } } @SuppressLint("ForegroundServiceType") override fun onCreate() { super.onCreate() createNotificationChannel() startForeground(NOTIFICATION_ID, createNotification()) } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { return START_STICKY } override fun onBind(intent: Intent?): IBinder? = null private fun createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel( CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW ).apply { description = "后台定位服务通知" setShowBadge(false) } val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager manager.createNotificationChannel(channel) } } private fun createNotification(): Notification { val builder = NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("定位服务运行中") .setContentText("正在后台获取位置信息") .setSmallIcon(android.R.drawable.ic_menu_mylocation) .setPriority(NotificationCompat.PRIORITY_LOW) .setOngoing(true) return builder.build() } }