AsyncTask wywołany z Handler nie wykona doInBackground

Aplikacja, nad którą pracuję, używa wątku w tle, aby pobrać listę obrazów za pośrednictwem interfejsu API, a następnie wyświetla obrazy w pokazie slajdów.

Istnieje zadanie w tle (obecnie AsyncTask) do okresowego pobierania nowych obrazów.

Nie otrzymuję żadnych komunikatów o błędach dotyczących niewłaściwego wątku itp., Po prostu druga instancja AsyncTasks nie uruchomi metody doInBackground.

Oto kod z działania:

<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>

TheDownloadTask wygląda tak:

<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>

Program obsługi zostanie wywołany, zostanie wywołany onPreExecute (w AsyncTask) i początkowy przebieg DownloadTask (bezpośrednio wonCreate) działa również.

Zgodnie z tym pytaniem:Android SDK AsyncTask doInBackground nie działa (podklasa) , może to być związane z SDK15.

Dzięki za wszelkie wskazówki.

Aktualizacja Gdy otrzymałem komentarze, Handler mógł nie znajdować się w wątku interfejsu użytkownika (co jest dziwne, podobnie jakThread.currentThread jest taki sam zarówno na stronie Cateate, jak i HandlershandleMessage metoda, poprawiłemhandleMessage metoda:

<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>

Nadal bez powodzenia.

Aktualizacja pełna klasa 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>

questionAnswers(2)

yourAnswerToTheQuestion