如何停止在Android的线程(进度) [英] How to stop a thread(progressbar) in android

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

问题描述

我已经使用进度和线程做了一个倒数计时器,现在我想停在当上button.I用户点击已经尝试使用Thread.stop()的同时进步,但它说,有这样的。没有方法我都试过interruot方法太没有运气,所以可以在任何哥们请我看code.My code是如下来帮助我。
code

 包com.amar.lyricalgenius;进口com.amar.lyricalgenius.LoadingActivity.MyCountDownTimer;进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.os.CountDownTimer;
进口android.os.Handler;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.Window;
进口android.widget.Button;
进口android.widget.ImageButton;
进口android.widget.ImageView;
进口android.widget.ProgressBar;
进口android.widget.TextView;
进口android.widget.Toast;公共类SinglePlayerOption扩展活动实现OnClickListener {    //可见走了
    私人INT progressStatus = 0;
    私人处理程序处理程序=新的处理程序();
    我的意图;
    TextView的text_player_second;
    ImageView的vs_word;
    ImageView的player_second_pic,player_second_box;
    的ImageButton red_point1;
    TextView的text_number_pt1;
    TextView的text_number1;
    进度pg_loading;
    私人CountDownTimer countDownTimer;
    TextView的timer_text;
    私人最终长STARTTIME = 8 * 1000;
    私人最终长间隔= 1 * 1000;
    按钮opt_1,opt_2,opt_3,opt_4;
    螺纹splashThread;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        的setContentView(R.layout.single_player_option);        在里面();    }    私人无效的init(){
        // TODO自动生成方法存根
        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 =(按钮)findViewById(R.id.option_1);
        opt_2 =(按钮)findViewById(R.id.option_2);
        opt_3 =(按钮)findViewById(R.id.option_3);
        opt_4 =(按钮)findViewById(R.id.option_4);        opt_1.setOnClickListener(本);
        opt_2.setOnClickListener(本);
        opt_3.setOnClickListener(本);
        opt_4.setOnClickListener(本);
        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 =新MyCountDownTimer(startTime时,间隔);
        timer_text.setText(timer_text.getText()
                +将String.valueOf(的startTime / 1000));        countDownTimer.start();        新主题(新的Runnable接口(){
            公共无效的run(){
                而(progressStatus< 100){
                    progressStatus + = 1;
                    //更新进度条,并显示                    //在文本视图当前值
                    handler.post(新的Runnable(){
                        公共无效的run(){
                            pg_loading.setProgress(progressStatus);
                        }
                    });
                    尝试{
                        //休眠为200毫秒。                        //只是慢慢显示进度
                        视频下载(62);
                    }赶上(InterruptedException的E){
                        e.printStackTrace();
                    }
                }
            }
        })。开始();        splashThread =新主题(){
            公共无效的run(){
                尝试{
                    睡眠(6000);
                    // Utils.systemUpgrade(SplashActivity.this);
                }赶上(InterruptedException的E){
                    e.printStackTrace();
                }                意向意图= NULL;
                意图=新意图(SinglePlayerOption.this,
                        NoResponseActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(意向);                完();            }
        };
        splashThread.start();    }    @燮pressWarnings(德precation)
    @覆盖
    公共无效的onClick(视图v){
        // TODO自动生成方法存根
        我的意图;
        开关(v.getId()){
        案例R.id.option_1:
            splashThread.stop();
            countDownTimer.onFinish();
            Toast.makeText(SinglePlayerOption.this,
                    timer_text.getText()的toString(),1).show();
            I =新意图(SinglePlayerOption.this,
                    DialogLeaderboardActivity.class);
            startActivity(ⅰ);
            打破;
        }
    }    公共类MyCountDownTimer扩展CountDownTimer {
        公共MyCountDownTimer(长的startTime,长间隔){
            超(startTime时,间隔);
        }        @覆盖
        公共无效onFinish(){
            timer_text.setText(时间到!);
        }        @覆盖
        公共无效onTick(长millisUntilFinished){
            timer_text.setText(+ millisUntilFinished / 1000);
        }
    }
}


解决方案

 发个=新主题(新的Runnable(){
             公共无效的run(){.... th.start(); //开始 th.interrupt(); //此停止。

和使用

 而(progressStatus< 100安培;&安培;!(Thread.currentThread()isInterrupted())){....

而不是

 而(progressStatus< 100){....

i have made a countdown timer using progressbar and a thread,Now i want to stop the progress at the same time when user clicks on a button.I have tried thread.stop(),but it says there is .no such method,I have tried interruot method too with no luck,So can any buddy please help me by viewing my code.My code is as below: 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.

and use

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

instead of

 while (progressStatus < 100) {....

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

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