状态常驻通知(1/4):StatusService前台服务(specialUse,60s自刷新,START_STICKY)+Manifest声明

This commit is contained in:
2026-11-23 21:17:29 +08:00
parent c35682874e
commit 0d4b6ab9e4
2 changed files with 118 additions and 0 deletions
@@ -5,6 +5,8 @@
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"/>
<application
android:label="玄龙期货"
android:networkSecurityConfig="@xml/network_security_config"
@@ -44,6 +46,14 @@
android:showWhenLocked="true"
android:turnScreenOn="true"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>
<service
android:name=".StatusService"
android:exported="false"
android:foregroundServiceType="specialUse">
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="persistent_status_notification"/>
</service>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
@@ -0,0 +1,108 @@
package com.xuanlong.xuanlong_futures
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.content.pm.ServiceInfo
import android.os.Build
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
/** 常驻状态通知:只负责渲染。采集方写 prefs(flutter.xl_status_text),本服务 60s 自刷新;
* 原生 AlarmReceiver 另发 ACTION_REFRESH 即时刷新。START_STICKY 保证被杀后自动重建。 */
class StatusService : Service() {
companion object {
const val CHANNEL_ID = "status_v1"
const val ACTION_START = "com.xuanlong.xuanlong_futures.status.START"
const val ACTION_REFRESH = "com.xuanlong.xuanlong_futures.status.REFRESH"
const val ACTION_STOP = "com.xuanlong.xuanlong_futures.status.STOP"
private const val NOTIF_ID = 20261123
private const val PREFS_FILE = "FlutterSharedPreferences"
private const val KEY_TEXT = "flutter.xl_status_text"
}
private val handler = Handler(Looper.getMainLooper())
private val tick = object : Runnable {
override fun run() {
refreshNotification()
handler.postDelayed(this, 60_000L)
}
}
override fun onCreate() {
super.onCreate()
createChannel()
startAsForeground()
handler.post(tick)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent?.action == ACTION_STOP) {
handler.removeCallbacks(tick)
stopForeground(STOP_FOREGROUND_REMOVE)
stopSelf()
return START_NOT_STICKY
}
refreshNotification()
return START_STICKY
}
override fun onDestroy() {
handler.removeCallbacks(tick)
super.onDestroy()
}
override fun onBind(intent: Intent?): IBinder? = null
private fun startAsForeground() {
val n = buildNotification()
if (Build.VERSION.SDK_INT >= 29) {
startForeground(NOTIF_ID, n, ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE)
} else {
startForeground(NOTIF_ID, n)
}
}
private fun refreshNotification() {
getSystemService(NotificationManager::class.java).notify(NOTIF_ID, buildNotification())
}
private fun statusText(): String {
val raw = getSharedPreferences(PREFS_FILE, MODE_PRIVATE).getString(KEY_TEXT, null)
if (!raw.isNullOrEmpty()) return raw
return "${nowHm()} 监控中 · 等待首次数据拉取"
}
private fun nowHm(): String = SimpleDateFormat("HH:mm", Locale.CHINA).format(Date())
private fun buildNotification(): Notification {
val pi = PendingIntent.getActivity(
this, 20261123,
Intent(this, MainActivity::class.java),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
)
return Notification.Builder(this, CHANNEL_ID)
.setContentTitle("玄龙期货 · 监控中")
.setContentText(statusText())
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setContentIntent(pi)
.build()
}
private fun createChannel() {
val ch = NotificationChannel(CHANNEL_ID, "状态常驻", NotificationManager.IMPORTANCE_LOW).apply {
setShowBadge(false)
lockscreenVisibility = Notification.VISIBILITY_PUBLIC
}
getSystemService(NotificationManager::class.java).createNotificationChannel(ch)
}
}