ekasetiawans / flutter_background_service

264 stars 183 forks source link

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

Open unleed-l opened 3 weeks ago

unleed-l commented 3 weeks 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 1 week 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-l commented 1 week 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-l commented 1 week 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)
  }
}