Skip to content

Commit

Permalink
[Android] Fix crashlytics issue with service on Android 12+
Browse files Browse the repository at this point in the history
  • Loading branch information
CoThomas committed Mar 12, 2024
1 parent 9201927 commit 0548db9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ 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() {
override fun onReceive(context: Context, intent: Intent) {
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 0548db9

Please sign in to comment.