0
public class MainActivity extends Activity implements Runnable{

    private int progressBarStatus = 0;
    private Handler progressBarHandler = new Handler();
    ProgressBar linProgressBar;
    private long fileSize = 0;
    Thread t1;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button b1 = (Button)findViewById(R.id.button1);
       Button b2 = (Button)findViewById(R.id.button2);

        b2.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View arg0) 
            {

                linProgressBar.setProgress(0);
                t1.interrupt();
            }

         });

    b1.setOnClickListener(new OnClickListener()
     {

        @Override
        public void onClick(View arg0) 
        {

            basicInitializations();

        }

     });

    }


    public void basicInitializations(){

        linProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
        linProgressBar.setProgress(0);
        linProgressBar.setMax(100);

        try{
            t1 = new Thread() 
            {
                public void run() {
                    while (progressBarStatus < 100) {

                        // process some tasks
                        progressBarStatus = doSomeTasks();

                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        // Update the progress bar
                        progressBarHandler.post(new Runnable() {
                            public void run() {
                                linProgressBar.setProgress(progressBarStatus);
                            }
                        });
                    }

                    if (progressBarStatus >= 100) {

                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) 
                        {
                            e.printStackTrace();
                        }

                    }
                }
            };

            t1.start();

        }catch (Exception e) {

        }


    }
    public int doSomeTasks() {

        while (fileSize <= 1000000) {

            fileSize++;

            if (fileSize == 100000) {
                return 10;
            } else if (fileSize == 200000) {
                return 20;
            } else if (fileSize == 300000) {
                return 30;
            }else if (fileSize == 400000) {
                return 40;
            }else if (fileSize == 500000) {
                return 50;
            }

        }

        return 100;

    }


    @Override
    public void run() {
        // TODO Auto-generated method stub

    }
}

这是我的主要代码,我无法停止这个线程。当我单击按钮 b2(比如取消按钮)时,我想停止它。上面的代码不是原始代码,是一个模型,所以请告诉我如何停止那个线程。

先感谢您 。. . .

4

2 回答 2

0

您的线程大部分时间都花在 中Thread.sleep(),这很有帮助。线程指针t1是一个类成员,这也很有帮助。从您的按钮处理程序中,您可以检查是否t1还活着,如果是,请调用t1.interrupt();. 这将导致线程当前的睡眠和未来的睡眠抛出InterruptedException... 现在您只需要修改您的异常处理程序以在这种情况下退出线程。

于 2012-08-21T11:46:34.847 回答
0

我有一个替代答案可以尝试相同的方法,在您的可运行循环中始终将标志检查为 run_flag,将其创建为成员变量并在您盯着循环后将其设置为 true,您可以将 run_flag 设置为 false,只要您完成并且同时,您也可以将 null 设置为您的线程......通过可运行循环和线程出来的更安全的方法。

try{
        t1 = new Thread() 
        {
            public void run() {
                while (progressBarStatus < 100 && run_flag == true) { // added run_flag here

                    // process some tasks
                    progressBarStatus = doSomeTasks();

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    // Update the progress bar
                    progressBarHandler.post(new Runnable() {
                        public void run() {
                            linProgressBar.setProgress(progressBarStatus);
                        }
                    });
                }

                if (progressBarStatus >= 100) {

                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) 
                    {
                        e.printStackTrace();
                    }

                }
            }
        };

        t1.start();

    }catch (Exception e) {

    }
于 2012-08-21T11:59:44.197 回答