Cannot reopen app when there is service doing CPU intensive task

58 Views Asked by At

My app has a foreground service running a CPU intensive task. The task is written in RenderScript and takes 10 to 15 minutes to finish. It is run on Dispatchers.Default so it won't block the UI. When the service started, I can still interact with the UI, even after I left the app and came back later, there is no problem. However, if I close the app (service still running) and reopen it, it will stuck at an empty screen, several seconds later, an alert will ask me to close the app or wait.

There is no bind service or anything related to the service in the starting page, but it still won't start. Seems like the background task is blocking it, but the task doesn't run on UI thread, how could it block the UI?

This is the code I used to run the task:

CoroutineScope(Dispatchers.Default).launch {
    my task...
    ...
}
1

There are 1 best solutions below

0
Jeffrey Chen On

I found the problem, in my MainActivity onCreate, I called:

rs = RenderScript.create(this)
script = ScriptC_singlesource(rs)

I think it would be useful to know when RenderScript is busy (even in other part of your app), calling this will block the thread until the job finished, so I'll leave this post here.