在Android中使用处理程序时如何停止线程?

时间:2018-03-19 02:43:54

标签: android multithreading handler

我打算制作几个由Handler点击时要更改的图像。 但是现在测试只有两个图像。 我想在两个点击它们的过程中更改图像。 再次单击图像时应停止第一个过程。 但是,在我的代码中,第一个过程并没有停止。 我可以看到这两个过程同时发挥作用。

如何停止第一个线程,如果有多个线程正在进行,我该如何逐个管理呢?

以下是我的代码。

    public class MainActivity extends AppCompatActivity {

    ImageView img;
    ImageView img2;
    ImageView img3;

    Button btn;
    Handler myHandler;
    Runnable myRun;
    int flag_number;

 int img1_phase = 0;

    //click method for image 1
    public void img1_click (View view) {
        flag_number = 1;
        click_change_Image(img, img1_phase);

        myRun = new Runnable() {
            @Override
            public void run() {
                time_change_image(img, img1_phase);
            }
        };
        new Thread(myRun).start();

        }
    }



//Click method for 2nd process
    public void click_change_Image(final ImageView imageView, int phase) {

        if (phase == 4 || phase == 5) {
            imageView.setImageResource(R.drawable.second_1);
            myHandler.removeCallbacksAndMessages(null); //this code doesn't work
            Thread.interrupted();// not work either
            phase = 7;
        }

        image_checker(phase);

        return;
    }



  //Change Image Process by Thread after Clicked
public void time_change_image(final ImageView imageView, int phase) {
        if (phase == 0) {
            change_Image(imageView, R.drawable.new_one_1, 300);
            phase = 1;
            image_checker(phase);

            change_Image(imageView, R.drawable.new_one_2, 300);
            phase = 2;
            image_checker(phase);

            change_Image(imageView, R.drawable.first_1, 300);
            phase = 3;
            image_checker(phase);

            change_Image(imageView, R.drawable.first_2, 3000);
            phase = 4;
            image_checker(phase);

            change_Image(imageView, R.drawable.first_3, 3000);
            phase = 5;
            image_checker(phase);

            change_Image(imageView, R.drawable.first_4, 3000);
            phase = 6;
            image_checker(phase);

            change_Image(imageView, R.drawable.empty, 3000);
            phase = 0;
            image_checker(phase);

        }else if(phase == 7) {

            change_Image(imageView, R.drawable.second_2, 3000);
            phase = 8;
            image_checker(phase);

            change_Image(imageView, R.drawable.second_3, 3000);
            phase = 9;
            image_checker(phase);

            change_Image(imageView, R.drawable.second_4, 3000);
            phase = 10;
            image_checker(phase);

            change_Image(imageView, R.drawable.empty, 3000);
            phase = 0;
            image_checker(phase);

        }

        flag_number = 0;

    }



 //Change Image Resource method
    public void change_Image(final ImageView iv, final int image, int wait_time) {
            sleep_Thread(wait_time);
            myHandler.post(new Runnable() {
                @Override
                public void run() {
                    iv.setImageResource(image);
                }
            });
        }
    }

2 个答案:

答案 0 :(得分:0)

使用标志来管理您的线程。例如:

boolean isRun = true (global variable)
   myRun = new Runnable() {
        @Override
        public void run() {
            if(isRun) {
               time_change_image(img, img1_phase);
            }
        }
    };

答案 1 :(得分:0)

使用Handler避免主线程延迟。

Handler mHandler = new Handler();

开始一个帖子:

 mHandler.post(Runnable);

mHandler.postDelayed(Runnable, some delayTime)

停止

 mHandler.removeCallback(Runnable);