I want to load some resources in parallel so that they load faster in a wall paper. So I decided to use the AsyncTask in Android wallpaperservice. Below is the code i used. I was shocked to know that the AsyncTask is called synchronously and also that the onPostExecute is never executed. What is the reason? Is there any alternative?
@Override
public Engine onCreateEngine() {
Log.d("PER", "onCreateEngine");
new DownloadWebPageTask().doInBackground("test");
for (int i = 0; i < 30; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("ASYNC", "DownloadWebPageTask aftersleep outside thread");
}
return new CubeEngine();
}
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = "";
Log.d("ASYNC", "DownloadWebPageTask start");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("ASYNC", "DownloadWebPageTask aftersleep inside thread");
return response;
}
@Override
protected void onPostExecute(String result) {
Log.d("ASYNC", "DownloadWebPageTask end");
}
}
You should never call
doInBackgrounddirectly. You need to callexecutelike it states in the docs. That is why your task is executing on the same thread.