0

I create a thread for animated change image on ui scheduled. It will be interrupt while button click, and set an fixed image. But if setImage in runOnUiThread accidentally happen after setImage in onClick, it will be the wrong image.

Is there any way in this code can make sure go through somewhere after return from InterruptedException. (I don't want any more delay on ui thread)

Please help thanks!

    thread = new Thread(){
        @Override
        public void run() {
            while(!this.isInterrupted()){
                for(int i=0; i<imageList.size(); i++){
                    if(getActivity()==null) return;
                    getActivity().runOnUiThread(new Runnable() {
                        public void run() {
                            ImageViewAnimatedChange(getActivity().getApplication(),
                                        imgFunction, imageList.get(j), 0);
                        }
                    });
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        Log.e(TAG, e.toString());
                        return;
                    }
                }
            }
        }
    };

//once onClick

                        if(thread!=null) {
                            thread.interrupt(); // catch interruptException
                            thread = null;
                            imgFunction.setImageResource(R.drawable.selector);
                        }

Edit: I rewrite this part as suggested.

public class asyncTask extends AsyncTask<String, String, String>{
    @Override
    protected String doInBackground(String... params) {
        Log.d(TAG, "doInBackground");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            Log.w(TAG, e.toString());
            return null;
        }

        final List<Bitmap> imageList = getImageList();
        if(imageList.isEmpty()) return null;
        Log.d(TAG, "start while ");

        while(!isCancelled()){
            Log.d(TAG, "while:" +Boolean.toString(isCancelled()));

            for(int i=0; i<imageList.size()+1; i++){
                final int j=i;
                Log.d(TAG, "for" + Integer.toString(j));

                if (j == imageList.size())
                    publishProgress(“ok”);
                else
                    publishProgress(Integer.toString(i));

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    Log.w(TAG, e.toString());
                    return null;
                }
            }
        }
        Log.d(TAG, "while end");
        return null;
    }
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        QUtil.logE(TAG, "onPostExecute");
        imgFunction.setImageResource(R.drawable.selector);
    }

    @Override
    protected void onProgressUpdate(String... value) {
        super.onProgressUpdate(value);
        Log.d(TAG, "onProgressUpdate" + value[0]);
        if(getActivity()==null || isCancelled()) return;
        if(value[0]==“ok”)
            ImageViewAnimatedChange(getActivity().getApplication(),
                    imgFunction, null, R.drawable.selector);
        else
            ImageViewAnimatedChange(getActivity().getApplication(),
                    imgFunction, getImageList().get(Integer.parseInt(value[0])), 0);
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
        Log.e(TAG, "onCancelled");
        try {
            Thread.sleep(60);
        } catch (InterruptedException e) {
            Log.w(TAG, e.toString());
        }
        imgFunction.setImageResource(R.drawable.selector);
    }
}

//start

iftttAsyncTask = new IftttAsyncTask().execute("");

// end onClick

if(asyncTask!=null) {
                            asyncTask.cancel(true); // catch interruptException
                            threadIfttt = null;
                        }

then sometimes work sometimes not work, not work while "doInBackground" is not in sleep tread! It did go in onCancel but not really cancel, I got false with isCanceled() Logs pics as follow enter image description here

Bubble.C
  • 70
  • 1
  • 9

1 Answers1

0

You may be better off using an AsyncTask for this type of background work, since it allows you to perform separate UI operations depending on whether the task proceeds or is interrupted.

For example, in this sample AsyncTask below:

public class TestTask extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... args) {

        String input = args[0];
        String output = "simulated return value";

        return output;
    }

    protected void onPostExecute(String result) {
        //if your background operation succeeds, you 
        //would update your image here
    }

    protected void onCancelled() {
       //if your task is cancelled, update your UI with the
       //default image here
    }
}

If the background operation succeeds, you would update your UI with the new image in onPostExecute(), which runs on the UI thread. If you interrupt the thread, such as with a button click, you would cancel the task, and instead of onPostExecute(), the onCancelled() method will be called instead. This method also runs on the UI thread, and you can just update your UI with the default image there.

NoChinDeluxe
  • 3,446
  • 1
  • 16
  • 29
  • hmmm.. kind of the same problem with AsyncTask, and what I need in AsyncTask onProgressUpdate not onPostExecute, it will end the while thread. The problem happen from void ImageViewAnimatedChange I made, this use anim_in and anim_out in it, so it still happen after I setImage(this without anim) – Bubble.C May 13 '16 at 06:15