mirror of
https://github.com/pppscn/SmsForwarder
synced 2025-08-03 09:27:41 +08:00
优化:部分手机通知栏异常显示两条常驻通知 #392
This commit is contained in:
parent
6ad1763d38
commit
dba756c2f4
@ -33,7 +33,7 @@ import kotlinx.coroutines.GlobalScope
|
|||||||
import kotlinx.coroutines.async
|
import kotlinx.coroutines.async
|
||||||
|
|
||||||
@SuppressLint("SimpleDateFormat")
|
@SuppressLint("SimpleDateFormat")
|
||||||
@Suppress("PrivatePropertyName", "DeferredResultUnused", "OPT_IN_USAGE", "DEPRECATION", "LiftReturnOrAssignment")
|
@Suppress("PrivatePropertyName", "DeferredResultUnused", "OPT_IN_USAGE", "DEPRECATION")
|
||||||
class ForegroundService : Service() {
|
class ForegroundService : Service() {
|
||||||
|
|
||||||
private val TAG: String = ForegroundService::class.java.simpleName
|
private val TAG: String = ForegroundService::class.java.simpleName
|
||||||
@ -85,7 +85,7 @@ class ForegroundService : Service() {
|
|||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
|
|
||||||
//纯客户端模式
|
//纯客户端模式
|
||||||
if (SettingUtils.enablePureClientMode) return START_STICKY
|
if (SettingUtils.enablePureClientMode) return START_NOT_STICKY
|
||||||
|
|
||||||
if (intent != null) {
|
if (intent != null) {
|
||||||
when (intent.action) {
|
when (intent.action) {
|
||||||
@ -118,6 +118,9 @@ class ForegroundService : Service() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun startForegroundService() {
|
private fun startForegroundService() {
|
||||||
|
if (isRunning) return
|
||||||
|
isRunning = true
|
||||||
|
|
||||||
val notification = createNotification(SettingUtils.notifyContent)
|
val notification = createNotification(SettingUtils.notifyContent)
|
||||||
startForeground(NOTIFICATION_ID, notification)
|
startForeground(NOTIFICATION_ID, notification)
|
||||||
|
|
||||||
@ -164,12 +167,8 @@ class ForegroundService : Service() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
isRunning = true
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
e.printStackTrace()
|
handleException(e, "startForegroundService")
|
||||||
Log.e(TAG, "startForegroundService: $e")
|
|
||||||
isRunning = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -181,9 +180,7 @@ class ForegroundService : Service() {
|
|||||||
compositeDisposable.dispose()
|
compositeDisposable.dispose()
|
||||||
isRunning = false
|
isRunning = false
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
e.printStackTrace()
|
handleException(e, "stopForegroundService")
|
||||||
Log.e(TAG, "stopForegroundService: $e")
|
|
||||||
isRunning = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -212,8 +209,18 @@ class ForegroundService : Service() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun updateNotification(updatedContent: String) {
|
private fun updateNotification(updatedContent: String) {
|
||||||
val notification = createNotification(updatedContent)
|
try {
|
||||||
notificationManager?.notify(NOTIFICATION_ID, notification)
|
val notification = createNotification(updatedContent)
|
||||||
|
notificationManager?.notify(NOTIFICATION_ID, notification)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
handleException(e, "updateNotification")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun handleException(e: Exception, methodName: String) {
|
||||||
|
e.printStackTrace()
|
||||||
|
Log.e(TAG, "$methodName: $e")
|
||||||
|
isRunning = false
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user