From 0548db9a63c2ad6d3a08d01b3aa1d353c058b3ea Mon Sep 17 00:00:00 2001 From: Thomas COQUAN Date: Tue, 12 Mar 2024 15:19:49 +0100 Subject: [PATCH] [Android] Fix crashlytics issue with service on Android 12+ --- android/build.gradle | 5 +++++ .../gdelataillade/alarm/alarm/AlarmReceiver.kt | 7 ++++++- .../gdelataillade/alarm/alarm/AlarmWorker.kt | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 android/src/main/kotlin/com/gdelataillade/alarm/alarm/AlarmWorker.kt diff --git a/android/build.gradle b/android/build.gradle index 63665a97..d673ce51 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -46,3 +46,8 @@ android { minSdkVersion 19 } } + +dependencies { + implementation 'androidx.work:work-runtime:2.7.1' + implementation 'com.google.guava:guava:27.0.1-android' +} \ No newline at end of file diff --git a/android/src/main/kotlin/com/gdelataillade/alarm/alarm/AlarmReceiver.kt b/android/src/main/kotlin/com/gdelataillade/alarm/alarm/AlarmReceiver.kt index d51d7679..ed03c7c3 100644 --- a/android/src/main/kotlin/com/gdelataillade/alarm/alarm/AlarmReceiver.kt +++ b/android/src/main/kotlin/com/gdelataillade/alarm/alarm/AlarmReceiver.kt @@ -4,6 +4,8 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.os.Build +import androidx.work.OneTimeWorkRequest +import androidx.work.WorkManager import io.flutter.Log class AlarmReceiver : BroadcastReceiver() { @@ -11,7 +13,10 @@ class AlarmReceiver : BroadcastReceiver() { val serviceIntent = Intent(context, AlarmService::class.java) serviceIntent.putExtras(intent) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + val request = OneTimeWorkRequest.Builder(AlarmWorker::class.java).addTag(AlarmWorker.TAG).build() + WorkManager.getInstance(context).enqueue(request) + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(serviceIntent) } else { context.startService(serviceIntent) diff --git a/android/src/main/kotlin/com/gdelataillade/alarm/alarm/AlarmWorker.kt b/android/src/main/kotlin/com/gdelataillade/alarm/alarm/AlarmWorker.kt new file mode 100644 index 00000000..a722fd74 --- /dev/null +++ b/android/src/main/kotlin/com/gdelataillade/alarm/alarm/AlarmWorker.kt @@ -0,0 +1,17 @@ +package com.gdelataillade.alarm.alarm + +import android.content.Context +import androidx.work.Worker +import androidx.work.WorkerParameters + + +class AlarmWorker(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) { + companion object { + const val TAG = "AlarmWorker" + } + + override fun doWork(): Result { + //call methods to perform background task + return Result.success() + } +}