@@ -62,10 +62,7 @@ class ForegroundService : Service() {
6262 // Check if the given intent is a LaunchIntent.
6363 val isLaunchIntent = (intent.action == Intent .ACTION_MAIN ) &&
6464 (intent.categories?.contains(Intent .CATEGORY_LAUNCHER ) == true )
65- if (! isLaunchIntent) {
66- // Log.d(TAG, "not LaunchIntent")
67- return
68- }
65+ if (! isLaunchIntent) return
6966
7067 val data = intent.getStringExtra(INTENT_DATA_NAME )
7168 if (data == ACTION_NOTIFICATION_PRESSED ) {
@@ -134,55 +131,47 @@ class ForegroundService : Service() {
134131 val isSetStopWithTaskFlag = ForegroundServiceUtils .isSetStopWithTaskFlag(this )
135132
136133 if (action == ForegroundServiceAction .API_STOP ) {
137- RestartReceiver .cancelRestartAlarm(this )
138134 stopForegroundService()
139135 return START_NOT_STICKY
140136 }
141137
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
153143 }
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()
159149 createForegroundTask()
160- } else {
161- val prevEventAction = prevForegroundTaskOptions?.eventAction
162- val currEventAction = foregroundTaskOptions.eventAction
163- if (prevEventAction != currEventAction) {
164- updateForegroundTask()
165- }
166150 }
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) {
173156 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+ }
179163 }
180- } else {
164+ }
165+ ForegroundServiceAction .REBOOT ,
166+ ForegroundServiceAction .RESTART -> {
181167 startForegroundService()
182168 createForegroundTask()
169+ Log .d(TAG , " The service has been restarted by Android OS." )
183170 }
184- Log .d(TAG , " The service has been restarted by Android OS." )
185171 }
172+ } catch (e: Exception ) {
173+ Log .e(TAG , e.message, e)
174+ stopForegroundService()
186175 }
187176
188177 return if (isSetStopWithTaskFlag) {
@@ -207,9 +196,8 @@ class ForegroundService : Service() {
207196 if (::foregroundServiceStatus.isInitialized) {
208197 isCorrectlyStopped = foregroundServiceStatus.isCorrectlyStopped()
209198 }
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." )
213201 RestartReceiver .setRestartAlarm(this , 5000 )
214202 }
215203 }
@@ -281,6 +269,8 @@ class ForegroundService : Service() {
281269
282270 @SuppressLint(" WrongConstant" , " SuspiciousIndentation" )
283271 private fun startForegroundService () {
272+ RestartReceiver .cancelRestartAlarm(this )
273+
284274 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
285275 createNotificationChannel()
286276 }
@@ -304,6 +294,8 @@ class ForegroundService : Service() {
304294 }
305295
306296 private fun stopForegroundService () {
297+ RestartReceiver .cancelRestartAlarm(this )
298+
307299 releaseLockMode()
308300 stopForeground(true )
309301 stopSelf()
0 commit comments