去掉前台服务权限
This commit is contained in:
parent
b4a2bf3410
commit
1f07381e19
|
|
@ -6,11 +6,6 @@
|
||||||
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
|
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
|
||||||
<uses-permission android:name="android.permission.VIBRATE"/>
|
<uses-permission android:name="android.permission.VIBRATE"/>
|
||||||
|
|
||||||
|
|
||||||
<!-- 前台服务权限 -->
|
|
||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
|
||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
|
|
||||||
|
|
||||||
<!-- 网络权限(如果需要从网络获取通知内容) -->
|
<!-- 网络权限(如果需要从网络获取通知内容) -->
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
|
|
@ -56,8 +51,7 @@
|
||||||
<service
|
<service
|
||||||
android:name=".service.NotificationKeepAliveService"
|
android:name=".service.NotificationKeepAliveService"
|
||||||
android:enabled="true"
|
android:enabled="true"
|
||||||
android:exported="false"
|
android:exported="false" />
|
||||||
android:foregroundServiceType="specialUse" />
|
|
||||||
|
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,14 @@
|
||||||
package com.remax.notification.service
|
package com.remax.notification.service
|
||||||
|
|
||||||
import android.app.Notification
|
|
||||||
import android.app.Service
|
import android.app.Service
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Build
|
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
import com.remax.base.ext.KvLongDelegate
|
import com.remax.base.ext.KvLongDelegate
|
||||||
import com.remax.base.ext.canSendNotification
|
|
||||||
import com.remax.base.report.DataReportManager
|
import com.remax.base.report.DataReportManager
|
||||||
import com.remax.notification.check.NotificationCheckController
|
import com.remax.notification.check.NotificationCheckController
|
||||||
import com.remax.notification.controller.NotificationTriggerController
|
|
||||||
import com.remax.notification.controller.NotificationTriggerController.triggerFixTimeNotification
|
import com.remax.notification.controller.NotificationTriggerController.triggerFixTimeNotification
|
||||||
import com.remax.notification.timing.NotificationTimingController
|
import com.remax.notification.timing.NotificationTimingController
|
||||||
import com.remax.notification.utils.NotiLogger
|
import com.remax.notification.utils.NotiLogger
|
||||||
|
|
@ -25,8 +21,7 @@ import com.remax.notification.utils.NotiLogger
|
||||||
class NotificationKeepAliveService : Service() {
|
class NotificationKeepAliveService : Service() {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val NOTIFICATION_ID = NotificationTriggerController.getResidentNotificationId()
|
|
||||||
|
|
||||||
// 默认15分钟 = 900秒
|
// 默认15分钟 = 900秒
|
||||||
private const val DEFAULT_INTERVAL_SECONDS = 900L
|
private const val DEFAULT_INTERVAL_SECONDS = 900L
|
||||||
|
|
||||||
|
|
@ -66,15 +61,11 @@ class NotificationKeepAliveService : Service() {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
context.startService(intent)
|
||||||
context.startForegroundService(intent)
|
NotiLogger.d("启动服务,间隔: ${intervalSeconds}秒")
|
||||||
} else {
|
|
||||||
context.startService(intent)
|
|
||||||
}
|
|
||||||
NotiLogger.d("启动保活服务,间隔: ${intervalSeconds}秒")
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
DataReportManager.reportData("Notific_Show_Fail",mapOf("reason" to "alive_service_${e.message}"))
|
DataReportManager.reportData("Notific_Show_Fail",mapOf("reason" to "alive_service_${e.message}"))
|
||||||
NotiLogger.e("启动保活服务失败", e)
|
NotiLogger.e("启动服务失败", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -128,15 +119,15 @@ class NotificationKeepAliveService : Service() {
|
||||||
ACTION_START_SERVICE -> {
|
ACTION_START_SERVICE -> {
|
||||||
intervalSeconds =
|
intervalSeconds =
|
||||||
intent.getLongExtra(EXTRA_INTERVAL_SECONDS, defaultIntervalSeconds)
|
intent.getLongExtra(EXTRA_INTERVAL_SECONDS, defaultIntervalSeconds)
|
||||||
startForegroundService()
|
startRunning()
|
||||||
}
|
}
|
||||||
|
|
||||||
ACTION_STOP_SERVICE -> {
|
ACTION_STOP_SERVICE -> {
|
||||||
stopForegroundService()
|
stopRunning()
|
||||||
}
|
}
|
||||||
|
|
||||||
ACTION_UPDATE_NOTIFICATION -> {
|
ACTION_UPDATE_NOTIFICATION -> {
|
||||||
updateForegroundNotification()
|
updateRunningNotification()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return START_STICKY // 服务被杀死后自动重启
|
return START_STICKY // 服务被杀死后自动重启
|
||||||
|
|
@ -146,48 +137,34 @@ class NotificationKeepAliveService : Service() {
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
stopForegroundService()
|
stopRunning()
|
||||||
NotiLogger.d("保活服务销毁")
|
NotiLogger.d("保活服务销毁")
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 启动前台服务
|
* 启动服务
|
||||||
*/
|
*/
|
||||||
private fun startForegroundService() {
|
private fun startRunning() {
|
||||||
if (isServiceRunning) {
|
if (isServiceRunning) {
|
||||||
NotiLogger.d("保活服务已在运行中,刷新通知,间隔: ${intervalSeconds}秒")
|
NotiLogger.d("服务已在运行中,刷新通知,间隔: ${intervalSeconds}秒")
|
||||||
// 服务已运行,只刷新通知
|
// 服务已运行,只刷新通知
|
||||||
updateForegroundNotification()
|
updateRunningNotification()
|
||||||
triggerFixTimeNotification()
|
triggerFixTimeNotification()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查通知权限
|
|
||||||
val hasNotificationPermission = canSendNotification()
|
|
||||||
NotiLogger.d("通知权限状态: $hasNotificationPermission")
|
|
||||||
|
|
||||||
if (!hasNotificationPermission) {
|
|
||||||
NotiLogger.w("没有通知权限,前台服务通知可能不会显示")
|
|
||||||
}
|
|
||||||
|
|
||||||
isServiceRunning = true
|
isServiceRunning = true
|
||||||
|
|
||||||
// 创建前台通知
|
|
||||||
val notification = createForegroundNotification()
|
|
||||||
|
|
||||||
// 启动前台服务
|
|
||||||
startForeground(NOTIFICATION_ID, notification)
|
|
||||||
|
|
||||||
// 启动定时任务
|
// 启动定时任务
|
||||||
startKeepAliveTask()
|
startKeepAliveTask()
|
||||||
|
|
||||||
NotiLogger.d("保活服务启动成功,间隔: ${intervalSeconds}秒")
|
NotiLogger.d("服务启动成功,间隔: ${intervalSeconds}秒")
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 停止前台服务
|
* 停止服务
|
||||||
*/
|
*/
|
||||||
private fun stopForegroundService() {
|
private fun stopRunning() {
|
||||||
if (!isServiceRunning) {
|
if (!isServiceRunning) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -197,16 +174,9 @@ class NotificationKeepAliveService : Service() {
|
||||||
// 停止定时任务
|
// 停止定时任务
|
||||||
stopKeepAliveTask()
|
stopKeepAliveTask()
|
||||||
|
|
||||||
// 停止前台服务
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
||||||
stopForeground(STOP_FOREGROUND_REMOVE)
|
|
||||||
} else {
|
|
||||||
@Suppress("DEPRECATION")
|
|
||||||
stopForeground(true)
|
|
||||||
}
|
|
||||||
stopSelf()
|
stopSelf()
|
||||||
|
|
||||||
NotiLogger.d("保活服务停止")
|
NotiLogger.d("服务停止")
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -224,7 +194,7 @@ class NotificationKeepAliveService : Service() {
|
||||||
try {
|
try {
|
||||||
NotiLogger.d("执行保活任务")
|
NotiLogger.d("执行保活任务")
|
||||||
DataReportManager.reportData("Notific_Pull", mapOf("topic" to "timer"))
|
DataReportManager.reportData("Notific_Pull", mapOf("topic" to "timer"))
|
||||||
updateForegroundNotification()
|
updateRunningNotification()
|
||||||
triggerFixTimeNotification()
|
triggerFixTimeNotification()
|
||||||
// 尝试触发保活通知
|
// 尝试触发保活通知
|
||||||
NotificationTimingController.getInstance().triggerNotificationIfAllowed(
|
NotificationTimingController.getInstance().triggerNotificationIfAllowed(
|
||||||
|
|
@ -259,34 +229,10 @@ class NotificationKeepAliveService : Service() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 更新前台服务通知
|
* 更新服务通知
|
||||||
*/
|
*/
|
||||||
private fun updateForegroundNotification() {
|
private fun updateRunningNotification() {
|
||||||
if (!isServiceRunning) {
|
// 不再需要更新前台通知
|
||||||
NotiLogger.d("前台服务未运行,无法更新通知")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!canSendNotification()) {
|
|
||||||
NotiLogger.d("无通知权限,忽略本次更新通知")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
val newNotification = createForegroundNotification()
|
|
||||||
startForeground(NOTIFICATION_ID, newNotification)
|
|
||||||
NotiLogger.d("前台服务通知已更新")
|
|
||||||
} catch (e: Exception) {
|
|
||||||
NotiLogger.e("更新前台服务通知失败", e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建前台通知
|
|
||||||
*/
|
|
||||||
private fun createForegroundNotification(): Notification {
|
|
||||||
// 使用NotificationTriggerController提供的构建函数
|
|
||||||
return NotificationTriggerController.buildResidentNotification(this)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue