I had a same bug in the AsyncTask. So i prefer a Thread.
вторник, 27 марта 2012 г. 5:18:01 UTC+4 пользователь martypantsROK написал:
>
> I have an operation that consumes too much time for the UI thread and
> need to do it in another thread. I created a thread using AsyncTask
> and the thread loops until it finds and answer. When its done, I use
> postExecute to deal with the data and update views. The problem is
> that the thread intermittently stops in the middle of a long loop with
> no errors. Sometimes it goes through all data looking, sometimes, the
> thread returns having only looped through a portion.
>
> I posted a question on Stackoverflow, but the only answer I got was
> not to use AsyncTask but just use threads without providing any
> reasoning or details why one is better than another.
>
> What do folks here think? Is there something wrong with the way I've
> implemented my AsyncTask? Or is the stackoverflow poster correct in
> that I should just blindly use Thread without understanding why this
> is incorrect, good, bad, ugly or whatever.
>
> private void playDeviceTile() {
>
> if (mDbHelper.isPlayingDevice(game_id)) {
>
> // make sure I'm seeing all played tiles
> refreshPlayedTiles();
>
> final Boolean found;
>
>
>
> final ProgressDialog dialog = new ProgressDialog(mcontext);
> dialog.setMessage("Android is thinking...");
> dialog.show();
>
> new AsyncTask<Void,Void, Boolean>(){
> @Override
> protected void onPostExecute(Boolean isFound) {
> if (!isFound) {
>
> passPlay(1); // never found a tile to
> play. We have to pass
> }
> else {
> playTile(currentTile,1);
>
> }
> dialog.dismiss();
> postInvalidate();
> invalidate();
> }
>
> @Override
> protected Boolean doInBackground(Void... params) {
> try {
> return doSearchforSpot();
> } catch (Exception e) {
> Log.e("DRAW", "Exception find spot for device
> tile", e);
> }
> return null;
> }
>
> }.execute();
>
> }
> }
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en