I have such task: need to download images from the internet and set them as view's background every 15 minutes (user sets the period). I have done something like that:
I have JobService
, it downloads images and saves them as files to created directory. After it send broadcast, receiver takes it and sets file names to it's listener. Listener - my class LauncherApplication
extends Application, it is created before all activities, I save it's instance and thus I can load images from files in every part of program.I do it with such AsyncTask
, execute it if necessary in OnResume()
:
public class BackgroundImageAsyncChanger extends AsyncTask<String, Void ,Drawable> {
private int pictureNumber;
private View backgroundView;
private Context context;
public BackgroundImageAsyncChanger(View backgroundView, Context context, int pictureNumber) {
this.backgroundView = backgroundView;
this.context = context;
this.pictureNumber = pictureNumber;
}
@Override
protected Drawable doInBackground(String ... imageFilesNames) {
final int index = pictureNumber;
final Bitmap bitmap = ImageFileOperator.getInstance().loadImage(context, imageFilesNames[index]);
final Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
return drawable;
}
@Override
protected void onPostExecute(Drawable backgroundImage) {
backgroundView.setBackground(backgroundImage);
}
}
And so there is the problem. It's takes time to download images from the Internet, and when they are downloaded after the Activity/Fragment OnResume() , it doesn't change background. How I can implement it better?
Thanks every one for answers!