Skip to content

Commit f960ee2

Browse files
committed
fix: Prevent crash by catching exceptions during foreground service start #318 #320 #331
1 parent 81e0258 commit f960ee2

File tree

3 files changed

+63
-49
lines changed

3 files changed

+63
-49
lines changed

android/src/main/kotlin/com/pravera/flutter_foreground_task/service/ForegroundService.kt

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -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()

android/src/main/kotlin/com/pravera/flutter_foreground_task/service/RebootReceiver.kt

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.pravera.flutter_foreground_task.service
22

3+
import android.app.ForegroundServiceStartNotAllowedException
34
import android.content.BroadcastReceiver
45
import android.content.Context
56
import android.content.Intent
7+
import android.os.Build
8+
import android.util.Log
69
import androidx.core.content.ContextCompat
710
import com.pravera.flutter_foreground_task.models.ForegroundServiceAction
811
import com.pravera.flutter_foreground_task.models.ForegroundServiceStatus
@@ -16,6 +19,10 @@ import com.pravera.flutter_foreground_task.utils.ForegroundServiceUtils
1619
* @version 1.0
1720
*/
1821
class RebootReceiver : BroadcastReceiver() {
22+
companion object {
23+
private val TAG = RebootReceiver::class.java.simpleName
24+
}
25+
1926
override fun onReceive(context: Context?, intent: Intent?) {
2027
if (context == null || intent == null) return
2128

@@ -45,9 +52,24 @@ class RebootReceiver : BroadcastReceiver() {
4552
}
4653

4754
private fun startForegroundService(context: Context) {
48-
// Create an intent for calling the service and store the action to be executed
49-
val nIntent = Intent(context, ForegroundService::class.java)
50-
ForegroundServiceStatus.setData(context, ForegroundServiceAction.REBOOT)
51-
ContextCompat.startForegroundService(context, nIntent)
55+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
56+
try {
57+
val nIntent = Intent(context, ForegroundService::class.java)
58+
ForegroundServiceStatus.setData(context, ForegroundServiceAction.REBOOT)
59+
ContextCompat.startForegroundService(context, nIntent)
60+
} catch (e: ForegroundServiceStartNotAllowedException) {
61+
Log.e(TAG, "Foreground service start not allowed exception: ${e.message}")
62+
} catch (e: Exception) {
63+
Log.e(TAG, e.message, e)
64+
}
65+
} else {
66+
try {
67+
val nIntent = Intent(context, ForegroundService::class.java)
68+
ForegroundServiceStatus.setData(context, ForegroundServiceAction.REBOOT)
69+
ContextCompat.startForegroundService(context, nIntent)
70+
} catch (e: Exception) {
71+
Log.e(TAG, e.message, e)
72+
}
73+
}
5274
}
5375
}

android/src/main/kotlin/com/pravera/flutter_foreground_task/service/RestartReceiver.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ class RestartReceiver : BroadcastReceiver() {
8888
} catch (e: ForegroundServiceStartNotAllowedException) {
8989
Log.e(TAG, "Foreground service start not allowed exception: ${e.message}")
9090
} catch (e: Exception) {
91-
Log.e(TAG, e.toString())
91+
Log.e(TAG, e.message, e)
9292
}
9393
} else {
9494
try {
9595
val nIntent = Intent(context, ForegroundService::class.java)
9696
ForegroundServiceStatus.setData(context, ForegroundServiceAction.RESTART)
9797
ContextCompat.startForegroundService(context, nIntent)
9898
} catch (e: Exception) {
99-
Log.e(TAG, e.toString())
99+
Log.e(TAG, e.message, e)
100100
}
101101
}
102102
}

0 commit comments

Comments
 (0)