Skip to content

Commit

Permalink
fix: Handle exceptions that occur when service component cannot be found
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev-hwang committed Oct 3, 2024
1 parent dc012e5 commit db2abe4
Showing 1 changed file with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}

0 comments on commit db2abe4

Please sign in to comment.