繁体   English   中英

如何在Android中停止线程(进度条)

[英]How to stop a thread(progressbar) in android

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

    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);
        }
    }
}
 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) {....

现在我想在用户单击按钮的同时停止进度。我已经尝试过thread.stop()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM