停止线程在Android应用程序 [英] Stop Thread in Android App

查看:133
本文介绍了停止线程在Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经寻找了几个小时,在谷歌和就在这里。我一直无法找到一个解决我的问题。我想线程,当用户presses后退按钮停止。因此,例如,用户将preSS后退按钮和循环将停止翻转硬币。然后textviews将填充环路成功地完成之前,用户取消了该操作的头和尾巴的数量。

我知道我必须设置

  dialog.setCancelable(假);
 

  dialog.setCancelable(真正的);
 

和我需要实现

  progressDialog.setOnCancelListener(新OnCancelListener(){

  公共无效OnCancel的(DialogInterface对话){

   background.setRunning(假);

  }});
 

但是,当我试图做到这一点最终杀死了我的整个应用程序的力量将其关闭。 能否请你帮忙。我还在一种新到Android编程和我渴望学得更多,如果你发现任何其他的东西,在$ C $词可以改善这将是AP preciated。

 包com.michaelpeerman.probability;

进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.os.Bundle;
进口android.os.Handler;
进口android.os.Message;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;
导入了java.util.Random;

公共类ProbabilityActivity扩展活动实现OnClickListener {

    私人按钮提交;
    ProgressDialog对话框;
    INT增量;
    主题背景;
    诠释头= 0;
    诠释尾= 0;

    公共无效的onCreate(包paramBundle){
        super.onCreate(paramBundle);
        的setContentView(R.layout.main);
        提交=((按钮)findViewById(R.id.submit));
        submit.setOnClickListener(本);
    }

    公共无效的onClick(视图查看){
        增量= 1;
        对话框=新ProgressDialog(本);
        dialog.setCancelable(假);
        dialog.setMessage(翻转硬币......);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setProgress(0);
        最大的EditText =(EditText上)findViewById(R.id.number);
        INT最大=的Integer.parseInt(max.getText()的toString());
        dialog.setMax(最大);
        dialog.show();
        背景=新主题(新的Runnable(){
            公共无效的run(){
                对于(INT J = 0; J< dialog.getMax(); J ++){
                    INT I = 1 +新的随机()nextInt(2)。
                    如果(ⅰ== 1)
                        头++;
                    如果(ⅰ== 2)
                        尾巴++;
                progressHandler.sendMessage(progressHandler.obtainMessage());
            }
        }
    });
    background.start();
}

处理器progressHandler =新的处理程序(){
    公共无效的handleMessage(信息MSG){

        dialog.incrementProgressBy(增量);
        如果(dialog.getProgress()== dialog.getMax()){
            dialog.dismiss();
            TextView的结果=(TextView中)findViewById(R.id.result);
            result.setText(头:+头+\ ntails:+尾);

        }
    }

};

}
 

解决方案

我很好奇你是如何code编译,因为 setRunning(布尔)不是一部分该API的

不管是,这里有一种方法退出线程干净。你的后台线程定义改成这样:

 背景=新主题(新的Runnable(){
    公共无效的run(){
        为(诠释J = 0;!Thread.interrupted()&安培;&安培; J&其中; dialog.getMax(); J ++){
            INT I = 1 +新的随机()nextInt(2)。
            如果(ⅰ== 1)
                头++;
            如果(ⅰ== 2)
                尾巴++;
            progressHandler.sendMessage(progressHandler.obtainMessage());
        }
    }
}
 

然后取消螺纹:

  background.interrupt();
 

I Have searched for a few hours on google and on here. I have been unable to find a solution to my problem. I want the thread to stop when the user presses the back button. So for example the user will press the back button and the loop will stop flipping coins. Then the textviews will be populated with the number of heads and tails that the loop managed to complete before the user canceled the operation.

I know i have to set

dialog.setCancelable(false);

to

dialog.setCancelable(true);

and that i need to implement

progressDialog.setOnCancelListener(new OnCancelListener(){

  public void onCancel(DialogInterface dialog) {

   background.setRunning(false);

  }});

But when i try to do this it ends up killing my entire app force closing it. Can you please help. Im still kind of new to android programming and I am eager to learn more so if you notice any other things in the code i can improve it would be appreciated.

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);

        }
    }

};

}

解决方案

I'm curious how your code compiles, since setRunning(boolean) is not part of the api for Thread.

Regardless of that, here's one way to exit a thread cleanly. Change your background thread definition to this:

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());
        }
    }
}

Then cancel your thread with:

background.interrupt();

这篇关于停止线程在Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆