aikiOr
aikiOr

Reputation: 51

Failed to use Amplify storage with a periodic worker

In my android app I'm using Amplify in order to download files from S3 bucket.

There are two ways to start the download:

  1. By pressing a button - works well
  2. By a periodic worker which occasionally throws the following exception:
AmplifyException {message=Something went wrong with your AWS S3 Storage list operation, cause=java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.netafim.tech_app/com.amazonaws.mobileconnectors.s3.transferutility.TransferService }: app is in background uid UidRecord{e945dd1 u0a281 TRNB bg:+1m5s172ms idle change:idle procs:1 seq(0,0,0)}, recoverySuggestion=See attached exception for more information and suggestions}
        at com.amplifyframework.storage.s3.operation.AWSS3StorageListOperation.lambda$start$0$AWSS3StorageListOperation(AWSS3StorageListOperation.java:95)
        at com.amplifyframework.storage.s3.operation.-$$Lambda$AWSS3StorageListOperation$h_rYgEkUSCQ04HohVqKR6853m80.run(Unknown Source:2)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919)
     Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.netafim.tech_app/com.amazonaws.mobileconnectors.s3.transferutility.TransferService }: app is in background uid UidRecord{e945dd1 u0a281 TRNB bg:+1m5s172ms idle change:idle procs:1 seq(0,0,0)}
        at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1616)
        at android.app.ContextImpl.startService(ContextImpl.java:1571)
        at android.content.ContextWrapper.startService(ContextWrapper.java:669)
        at com.amplifyframework.storage.s3.service.AWSS3StorageService.startServiceIfNotAlreadyStarted(AWSS3StorageService.java:226)
        at com.amplifyframework.storage.s3.service.AWSS3StorageService.listFiles(AWSS3StorageService.java:157)
        at com.amplifyframework.storage.s3.operation.AWSS3StorageListOperation.lambda$start$0$AWSS3StorageListOperation(AWSS3StorageListOperation.java:91)
        at com.amplifyframework.storage.s3.operation.-$$Lambda$AWSS3StorageListOperation$h_rYgEkUSCQ04HohVqKR6853m80.run(Unknown Source:2) 
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462) 
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:919) 

As I understand it, the exception occurs because the Amplify package is trying to start a service while the app is in the background.

Currently I'm using RxAmplify for both cases. Is there a workaround that still use the RxAmplify library?

Upvotes: 1

Views: 390

Answers (1)

LaurieScheepers
LaurieScheepers

Reputation: 51

A simple solution to this is to use setForeground() in your CoroutineWorker.

It will launch a Foreground Service for you, that can run for longer than 10 minutes. This service can then launch the AWS internal service in the background. Tested and confirmed working in production.

Please see: https://developer.android.com/topic/libraries/architecture/workmanager/advanced/long-running

Upvotes: 0

Related Questions