如何在android中停止一个线程(进度条)

时间:2014-12-08 08:46:21

标签: android multithreading

我使用进度条和线程制作倒计时器,现在我想在用户点击按钮的同时停止进度。我已经尝试过thread.stop(),但它说有.no这样的方法,我也试过interruot方法也没有运气,所以任何伙伴都可以通过查看我的代码来帮助我。我的代码如下: 的

    package com.amar.lyricalgenius;

import com.amar.lyricalgenius.LoadingActivity.MyCountDownTimer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class SinglePlayerOption extends Activity implements OnClickListener {

    // visible gone
    private int progressStatus = 0;
    private Handler handler = new Handler();
    Intent i;
    TextView text_player_second;
    ImageView vs_word;
    ImageView player_second_pic, player_second_box;
    ImageButton red_point1;
    TextView text_number_pt1;
    TextView text_number1;
    ProgressBar pg_loading;
    private CountDownTimer countDownTimer;
    TextView timer_text;
    private final long startTime = 8 * 1000;
    private final long interval = 1 * 1000;
    Button opt_1, opt_2, opt_3, opt_4;
    Thread splashThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.single_player_option);

        init();

    }

    private void init() {
        // TODO Auto-generated method stub
        text_player_second = (TextView) findViewById(R.id.text_player_second);
        vs_word = (ImageView) findViewById(R.id.vs_word);
        player_second_pic = (ImageView) findViewById(R.id.player_second_pic);
        player_second_box = (ImageView) findViewById(R.id.player_second_box);

        red_point1 = (ImageButton) findViewById(R.id.red_point1);
        text_number_pt1 = (TextView) findViewById(R.id.text_number_pt1);
        text_number1 = (TextView) findViewById(R.id.text_number1);

        opt_1 = (Button) findViewById(R.id.option_1);
        opt_2 = (Button) findViewById(R.id.option_2);
        opt_3 = (Button) findViewById(R.id.option_3);
        opt_4 = (Button) findViewById(R.id.option_4);

        opt_1.setOnClickListener(this);
        opt_2.setOnClickListener(this);
        opt_3.setOnClickListener(this);
        opt_4.setOnClickListener(this);
        text_player_second.setVisibility(View.GONE);
        vs_word.setVisibility(View.GONE);
        player_second_pic.setVisibility(View.GONE);
        player_second_box.setVisibility(View.GONE);
        red_point1.setVisibility(View.GONE);
        text_number_pt1.setVisibility(View.GONE);
        text_number1.setVisibility(View.GONE);
        countDownTimer = new MyCountDownTimer(startTime, interval);
        timer_text.setText(timer_text.getText()
                + String.valueOf(startTime / 1000));

        countDownTimer.start();

        new Thread(new Runnable() {
            public void run() {
                while (progressStatus < 100) {
                    progressStatus += 1;
                    // Update the progress bar and display the

                    // current value in the text view
                    handler.post(new Runnable() {
                        public void run() {
                            pg_loading.setProgress(progressStatus);
                        }
                    });
                    try {
                        // Sleep for 200 milliseconds.

                        // Just to display the progress slowly
                        Thread.sleep(62);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        splashThread = new Thread() {
            public void run() {
                try {
                    sleep(6000);
                    // Utils.systemUpgrade(SplashActivity.this);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                Intent intent = null;
                intent = new Intent(SinglePlayerOption.this,
                        NoResponseActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(intent);

                finish();

            }
        };
        splashThread.start();

    }

    @SuppressWarnings("deprecation")
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent i;
        switch (v.getId()) {
        case R.id.option_1:
            splashThread.stop();
            countDownTimer.onFinish();
            Toast.makeText(SinglePlayerOption.this,
                    timer_text.getText().toString(), 1).show();
            i = new Intent(SinglePlayerOption.this,
                    DialogLeaderboardActivity.class);
            startActivity(i);
            break;
        }
    }

    public class MyCountDownTimer extends CountDownTimer {
        public MyCountDownTimer(long startTime, long interval) {
            super(startTime, interval);
        }

        @Override
        public void onFinish() {
            timer_text.setText("Time's up!");
        }

        @Override
        public void onTick(long millisUntilFinished) {
            timer_text.setText("" + millisUntilFinished / 1000);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

 Thread th = new Thread(new Runnable() {
             public void run() { ....

 th.start();//starts

 th.interrupt();//this stops.

并使用

 while (progressStatus < 100 && (!Thread.currentThread().isInterrupted())){....

而不是

 while (progressStatus < 100) {....

答案 1 :(得分:0)

  

现在我想在用户点击的同时停止进度   一个按钮。我试过thread.stop()

Thread.stop()已弃用,您不应该使用它。要理解的基本概念是,当执行其run方法的最后一行代码时,线程终止是执行。对于你的&#34; ProgressBar Thread *,当用户按下按钮时,你必须设置while循环(progressStatus = 100)的退出条件才能使其终止