@@ -62,10 +62,7 @@ class ForegroundService : Service() {
62
62
// Check if the given intent is a LaunchIntent.
63
63
val isLaunchIntent = (intent.action == Intent .ACTION_MAIN ) &&
64
64
(intent.categories?.contains(Intent .CATEGORY_LAUNCHER ) == true )
65
- if (! isLaunchIntent) {
66
- // Log.d(TAG, "not LaunchIntent")
67
- return
68
- }
65
+ if (! isLaunchIntent) return
69
66
70
67
val data = intent.getStringExtra(INTENT_DATA_NAME )
71
68
if (data == ACTION_NOTIFICATION_PRESSED ) {
@@ -134,55 +131,47 @@ class ForegroundService : Service() {
134
131
val isSetStopWithTaskFlag = ForegroundServiceUtils .isSetStopWithTaskFlag(this )
135
132
136
133
if (action == ForegroundServiceAction .API_STOP ) {
137
- RestartReceiver .cancelRestartAlarm(this )
138
134
stopForegroundService()
139
135
return START_NOT_STICKY
140
136
}
141
137
142
- if (intent == null ) {
143
- ForegroundServiceStatus .setData(this , ForegroundServiceAction .RESTART )
144
- foregroundServiceStatus = ForegroundServiceStatus .getData(this )
145
- action = foregroundServiceStatus.action
146
- }
147
-
148
- when (action) {
149
- ForegroundServiceAction .API_START ,
150
- ForegroundServiceAction .API_RESTART -> {
151
- startForegroundService()
152
- createForegroundTask()
138
+ try {
139
+ if (intent == null ) {
140
+ ForegroundServiceStatus .setData(this , ForegroundServiceAction .RESTART )
141
+ foregroundServiceStatus = ForegroundServiceStatus .getData(this )
142
+ action = foregroundServiceStatus.action
153
143
}
154
- ForegroundServiceAction . API_UPDATE -> {
155
- updateNotification()
156
- val prevCallbackHandle = prevForegroundTaskData?.callbackHandle
157
- val currCallbackHandle = foregroundTaskData.callbackHandle
158
- if (prevCallbackHandle != currCallbackHandle) {
144
+
145
+ when (action) {
146
+ ForegroundServiceAction . API_START ,
147
+ ForegroundServiceAction . API_RESTART -> {
148
+ startForegroundService()
159
149
createForegroundTask()
160
- } else {
161
- val prevEventAction = prevForegroundTaskOptions?.eventAction
162
- val currEventAction = foregroundTaskOptions.eventAction
163
- if (prevEventAction != currEventAction) {
164
- updateForegroundTask()
165
- }
166
150
}
167
- }
168
- ForegroundServiceAction .REBOOT ,
169
- ForegroundServiceAction .RESTART -> {
170
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
171
- try {
172
- startForegroundService()
151
+ ForegroundServiceAction .API_UPDATE -> {
152
+ updateNotification()
153
+ val prevCallbackHandle = prevForegroundTaskData?.callbackHandle
154
+ val currCallbackHandle = foregroundTaskData.callbackHandle
155
+ if (prevCallbackHandle != currCallbackHandle) {
173
156
createForegroundTask()
174
- } catch (e: ForegroundServiceStartNotAllowedException ) {
175
- Log .e(TAG ,
176
- " Cannot run service as foreground: $e for notification channel " )
177
- RestartReceiver .cancelRestartAlarm(this )
178
- stopForegroundService()
157
+ } else {
158
+ val prevEventAction = prevForegroundTaskOptions?.eventAction
159
+ val currEventAction = foregroundTaskOptions.eventAction
160
+ if (prevEventAction != currEventAction) {
161
+ updateForegroundTask()
162
+ }
179
163
}
180
- } else {
164
+ }
165
+ ForegroundServiceAction .REBOOT ,
166
+ ForegroundServiceAction .RESTART -> {
181
167
startForegroundService()
182
168
createForegroundTask()
169
+ Log .d(TAG , " The service has been restarted by Android OS." )
183
170
}
184
- Log .d(TAG , " The service has been restarted by Android OS." )
185
171
}
172
+ } catch (e: Exception ) {
173
+ Log .e(TAG , e.message, e)
174
+ stopForegroundService()
186
175
}
187
176
188
177
return if (isSetStopWithTaskFlag) {
@@ -207,9 +196,8 @@ class ForegroundService : Service() {
207
196
if (::foregroundServiceStatus.isInitialized) {
208
197
isCorrectlyStopped = foregroundServiceStatus.isCorrectlyStopped()
209
198
}
210
- val isSetStopWithTaskFlag = ForegroundServiceUtils .isSetStopWithTaskFlag(this )
211
- if (! isCorrectlyStopped && ! isSetStopWithTaskFlag) {
212
- Log .e(TAG , " The service was terminated due to an unexpected problem. The service will restart after 5 seconds." )
199
+ if (! isCorrectlyStopped && ! ForegroundServiceUtils .isSetStopWithTaskFlag(this )) {
200
+ Log .e(TAG , " The service will be restarted after 5 seconds because it wasn't properly stopped." )
213
201
RestartReceiver .setRestartAlarm(this , 5000 )
214
202
}
215
203
}
@@ -281,6 +269,8 @@ class ForegroundService : Service() {
281
269
282
270
@SuppressLint(" WrongConstant" , " SuspiciousIndentation" )
283
271
private fun startForegroundService () {
272
+ RestartReceiver .cancelRestartAlarm(this )
273
+
284
274
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
285
275
createNotificationChannel()
286
276
}
@@ -304,6 +294,8 @@ class ForegroundService : Service() {
304
294
}
305
295
306
296
private fun stopForegroundService () {
297
+ RestartReceiver .cancelRestartAlarm(this )
298
+
307
299
releaseLockMode()
308
300
stopForeground(true )
309
301
stopSelf()
0 commit comments