The application I'm working on is using a background thread to download a list of images through a API, then display the images in a slideshow.
There is a background task (currently AsyncTask) to periodically fetch the new images.
I'm not getting any error messages about the wrong Thread etc, it's just that the AsyncTasks's second instance will not run the doInBackground method.
Here is some code from the Activity:
private DownloadTask mDownloadTask = null;
private Handler mHandler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if(mDownloadTask != null) {
mDownloadTask.cancel(true);
}
mDownloadTask = new DownloadTask();
mDownloadTask.execute((Void[]) null);
}
};
mDownloadTask = new DownloadTask();
mDownloadTask.execute((Void[]) null);
}
The DownloadTask
looks like this:
@Override
protected List<String> doInBackground(Void... voids) {
// Download list of URLs from server, etc.
}
@Override
protected void onPostExecute(List<String> urls) {
mHandler.sendEmptyMessageDelayed(111, 5000);
}
The handler will be called, the onPreExecute (in the AsyncTask) will be called and the initial run of DownloadTask (right in onCreate
) also works.
According to this question: Android SDK AsyncTask doInBackground not running (subclass) , it might be SDK15 related.
Thanks for any hints.
Update As I received comments that the Handler might not be in the UI thread (which is weird, as Thread.currentThread
is the same both in onCreate and the Handlers handleMessage
method, I revised the handleMessage
method to:
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if(mDownloadTask != null) {
mDownloadTask.cancel(true);
}
mDownloadTask = new DownloadTask();
mDownloadTask.execute((Void[]) null);
}
});
}
};
Still without success.
Update the full DownloadTask class
class DownloadTask extends AsyncTask<Void, Void, List<String>> {
@Override
protected void onPreExecute() {
// Cancel the animation.
if (mSlideshowAnimation != null) {
mSlideshowAnimation.cancel(true);
}
mImageView1.setVisibility(View.GONE);
mImageView2.setVisibility(View.GONE);
animate(mProgressBar).alpha(1.0f).setDuration(500).start();
Log.d(TAG, "Download preparation done.");
}
@Override
protected List<String> doInBackground(Void... voids) {
Log.d(TAG, "Download");
SharedPreferences s = getSharedPreferences("access", Context.MODE_PRIVATE);
String token = s.getString("token", null);
Log.d(TAG, "Downloading slideshows.");
List<String> urls = new ArrayList<String>();
Slideshow[] slideshows = new Api(SlideshowActivity.this).getSlideshows(token);
for (Slideshow slideshow : slideshows) {
urls.addAll(slideshow.getAllPhotoUrls());
}
Log.d(TAG, "Downloading slideshows: " + slideshows.length);
for (String url : urls) {
try {
url = Api.HOST + url;
if (!Cache.fileExists(Cache.getCacheFilenameForUrl(SlideshowActivity.this, url))) {
Cache.cacheStream(SlideshowActivity.this, HttpHelper.download(SlideshowActivity.this, url), url);
} else {
Log.d(TAG, "Cached: " + url);
}
} catch (IOException e) {
Log.e(TAG, "Error while downloading.", e);
}
}
Log.d(TAG, "Downloading slideshows finished.");
return urls;
}
@Override
protected void onPostExecute(List<String> urls) {
Log.d(TAG, "download successful");
animate(mProgressBar).alpha(0.0f).setDuration(500).start();
mCurrentImageIndex = -1;
mImageUrls = urls;
mSlideshowAnimation = new SlideshowAnimation();
mSlideshowAnimation.execute((Void[]) null);
mHandler.sendEmptyMessageDelayed(111, 5000);
}
}
mDownloadTask = new DownloadTask();
both times? – Sebastian Roth.execute((Void[]) null)
. It can be written.execute()
. – Haulethunchecked varargs
parameter. – Sebastian Roth