diff --git a/android/src/main/kotlin/com/pravera/flutter_foreground_task/utils/ForegroundServiceUtils.kt b/android/src/main/kotlin/com/pravera/flutter_foreground_task/utils/ForegroundServiceUtils.kt index 0701542..fcaed8a 100644 --- a/android/src/main/kotlin/com/pravera/flutter_foreground_task/utils/ForegroundServiceUtils.kt +++ b/android/src/main/kotlin/com/pravera/flutter_foreground_task/utils/ForegroundServiceUtils.kt @@ -3,17 +3,28 @@ package com.pravera.flutter_foreground_task.utils import android.content.ComponentName import android.content.Context import android.content.pm.PackageManager +import android.content.pm.PackageManager.NameNotFoundException import android.content.pm.ServiceInfo +import android.util.Log import com.pravera.flutter_foreground_task.service.ForegroundService class ForegroundServiceUtils { companion object { - fun isSetStopWithTaskFlag(context: Context): Boolean { - val pm = context.packageManager - val cName = ComponentName(context, ForegroundService::class.java) - val flags = pm.getServiceInfo(cName, PackageManager.GET_META_DATA).flags + private val TAG = ForegroundServiceUtils::class.java.simpleName - return (flags and ServiceInfo.FLAG_STOP_WITH_TASK) == 1 + fun isSetStopWithTaskFlag(context: Context): Boolean { + return try { + val pm = context.packageManager + val cName = ComponentName(context, ForegroundService::class.java) + val flags = pm.getServiceInfo(cName, PackageManager.GET_META_DATA).flags + (flags and ServiceInfo.FLAG_STOP_WITH_TASK) == 1 + } catch (e: NameNotFoundException) { + Log.e(TAG, "isSetStopWithTaskFlag >> The service component cannot be found on the system.") + true + } catch (e: Exception) { + Log.e(TAG, "isSetStopWithTaskFlag >> $e") + true + } } } }