ekasetiawans / flutter_background_service

269 stars 192 forks source link

Is there a way to know if app is closed or not? #473

Open unleed opened 3 months ago

unleed commented 3 months ago

I need to run two tasks in the background, one needs to run even if the app is closed and the other cannot run if the app is closed. Is there a way to do this?

Henrikkee commented 2 months ago

You can implement the WidgetsBindingObserver interface to check if the app is in the resumed state or in the foreground before executing each task.

unleed commented 2 months ago

i tryed this, but it does not work when the app isn't resumed and you close the app from the recent apps.

unleed commented 2 months ago

But i already solved it creating a plugin to listen for app lifecycle using native code.

class AppStateCheckerPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
  private lateinit var channel : MethodChannel
  private var activity: Activity? = null
  private lateinit var sharedPreferences: SharedPreferences

  override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    channel = MethodChannel(flutterPluginBinding.binaryMessenger, "app_state_checker")
    channel.setMethodCallHandler(this)
    sharedPreferences = flutterPluginBinding.applicationContext.getSharedPreferences("AppStatePrefs", Context.MODE_PRIVATE)
  }

  override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
    when (call.method) {
      "isAppOpen" -> {
        val isAppOpen = sharedPreferences.getBoolean("isAppOpen", false)
        result.success(isAppOpen)
      }
      else -> result.notImplemented()
    }
  }

  override fun onAttachedToActivity(binding: ActivityPluginBinding) {
    activity = binding.activity
    setAppState(true)  // App foi aberto
  }

  override fun onDetachedFromActivityForConfigChanges() {
    setAppState(false)  // App foi fechado
  }

  override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
    activity = binding.activity
    setAppState(true)  // App foi reaberto
  }

  override fun onDetachedFromActivity() {
    setAppState(false)  // App foi fechado
  }

  private fun setAppState(isOpen: Boolean) {
    sharedPreferences.edit().putBoolean("isAppOpen", isOpen).apply()
  }

  override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
    channel.setMethodCallHandler(null)
  }
}