AsyncTask llamado desde Handler no ejecutará doInBackground
La aplicación en la que estoy trabajando está utilizando un hilo de fondo para descargar una lista de imágenes a través de una API y luego mostrar las imágenes en una presentación de diapositivas.
Hay una tarea en segundo plano (actualmente AsyncTask) para buscar periódicamente las nuevas imágenes.
No recibo ningún mensaje de error sobre el subproceso incorrecto, etc., solo que la segunda instancia de AsyncTasks no ejecutará el método doInBackground.
Aquí hay un código de la Actividad:
<code>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); } </code>
losDownloadTask
Se ve como esto:
<code>@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); } </code>
Se llamará al controlador, se llamará a onPreExecute (en AsyncTask) y la ejecución inicial de DownloadTask (a la derecha enonCreate
) también funciona.
Según esta pregunta:Android SDK AsyncTask doInBackground no se está ejecutando (subclase) , podría estar relacionado con SDK15.
Gracias por cualquier consejo.
Actualizar Cuando recibí comentarios de que el manejador podría no estar en el hilo de la interfaz de usuario (lo cual es extraño, comoThread.currentThread
Es el mismo tanto en onCreate como en los Handlers.handleMessage
Método, revisé elhandleMessage
método para:
<code>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); } }); } }; </code>
Todavía sin éxito.
Actualizar la clase completa de DownloadTask
<code>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); } } </code>