在Android应用中停止线程

时间:2012-03-07 03:56:03

标签: android

我在谷歌和这里搜索了几个小时。我一直无法找到解决问题的方法。我希望当用户按下后退按钮时线程停止。因此,例如用户将按下后退按钮,循环将停止翻转硬币。然后,文本视图将填充循环在用户取消操作之前完成的头部和尾部的数量。

我知道我必须设置

dialog.setCancelable(false);

dialog.setCancelable(true);

我需要实施

progressDialog.setOnCancelListener(new OnCancelListener(){

  public void onCancel(DialogInterface dialog) {

   background.setRunning(false);

  }});

但是当我尝试这样做时,最终会杀死我的整个应用程序关闭它。 你能帮忙吗?我仍然是Android编程的新手,我渴望了解更多,所以如果你发现代码中的任何其他东西我可以改进它将不胜感激。

package com.michaelpeerman.probability;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;

public class ProbabilityActivity extends Activity implements OnClickListener {

    private Button submit;
    ProgressDialog dialog;
    int increment;
    Thread background;
    int heads = 0;
    int tails = 0;

    public void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.main);
        submit = ((Button) findViewById(R.id.submit));
        submit.setOnClickListener(this);
    }

    public void onClick(View view) {
        increment = 1;
        dialog = new ProgressDialog(this);
        dialog.setCancelable(false);
        dialog.setMessage("Flipping Coin...");
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setProgress(0);
        EditText max = (EditText) findViewById(R.id.number);
        int maximum = Integer.parseInt(max.getText().toString());
        dialog.setMax(maximum);
        dialog.show();
        background = new Thread(new Runnable() {
            public void run() {
                for (int j = 0; j < dialog.getMax(); j++) {
                    int i = 1 + new Random().nextInt(2);
                    if (i == 1)
                        heads++;
                    if (i == 2)
                        tails++;
                progressHandler.sendMessage(progressHandler.obtainMessage());
            }
        }
    });
    background.start();
}

Handler progressHandler = new Handler() {
    public void handleMessage(Message msg) {

        dialog.incrementProgressBy(increment);
        if (dialog.getProgress() == dialog.getMax()) {
            dialog.dismiss();
            TextView result = (TextView) findViewById(R.id.result);
            result.setText("heads : " + heads + "\ntails : " + tails);

        }
    }

};

}

3 个答案:

答案 0 :(得分:5)

我很好奇你的代码是如何编译的,因为setRunning(boolean)不是Thread的api的一部分。

无论如何,这是一种彻底退出线程的方法。将后台线程定义更改为:

background = new Thread(new Runnable() {
    public void run() {
        for (int j = 0; !Thread.interrupted() && j < dialog.getMax(); j++) {
            int i = 1 + new Random().nextInt(2);
            if (i == 1)
                heads++;
            if (i == 2)
                tails++;
            progressHandler.sendMessage(progressHandler.obtainMessage());
        }
    }
}

然后用以下内容取消你的主题:

background.interrupt();

答案 1 :(得分:1)

您不应该强行关闭应用中的主题。这种线程管理通常是一个坏主意,通常完全不需要Android框架提供给你的那种细节。相反,你应该做的是使用AsyncTask来更新UI(这也消除了使用那个Messenger,虽然在这个例子中非常简单,但可能会变得更加糟糕!)。使用AsyncTask,您可以在后台执行所需的任何工作并同时更新UI。您可以在painless threading Android tutorial中阅读所有相关内容。

答案 2 :(得分:1)

要停止一个线程,你应该使用Thread.interrupt()方法,它不会立即停止线程,但它通知android系统需要停止该特定线程,并且当它适合于时,android os会中断该线程停止线程。