Saving AsyncTask in low memory conditions?

I wrote a home screen widget as an ImageView, i.e. The code generates the entire widget and writes it to RemoteViews as a bitmap. On slower devices with low memory, such as my G1, it may take a few seconds to generate a bitmap, and to avoid a terrible power closure, I moved all the graphic code to AsyncTask. So far so good.

However, in very low memory conditions, AsyncTask can easily be killed by the OS before it finishes creating a bitmap, resulting in an empty square on the desktop. Therefore, before creating AsyncTask, it sets the alarm for the moment + 5 seconds, and after the completion of the graphics, it resets the alarm. It is assumed that if an alarm is triggered, it is more than likely that the process was killed, and we must set another signal + 5 and start again.

This works, but in conditions of extreme low memory it may take several attempts (20 + seconds) to draw the widget, so I added the placeholder hourglass image as a static resource, which is displayed on the main screen until the real image is ready.

My question is: is this the best way? Is there a way (politely) to ask the OS not to kill AsyncTask until it runs out (on the grounds that this can lead to more system resources)?

I understand that if a process is classified as "visible", Android will not kill it, except in a pinch. But I do not know how to mark AsyncTask as "visible". I am very glad that the process was killed as soon as he drew the widget. It would be easy for him to survive long enough to complete.

+3
source share
3 answers

IntentService ? , , . , AsyncTask, , .

0

Service class, "Foreground". , Foreground.

, , Async.

, .

0

Source: https://habr.com/ru/post/1760991/


All Articles