jigar
jigar

Reputation: 11

How to stop a thread(progressbar) in android

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

Upvotes: 1

Views: 2229

Answers (2)

Blackbelt
Blackbelt

Reputation: 157487

Now i want to stop the progress at the same time when user clicks on a button.I have tried thread.stop()

Thread.stop() is deprecated and you should not use it. The basic concept to understand is that a thread terminates is execution when the last line of code of his run method is executed. In the case of your "ProgressBar Thread*, when the user press the button, you have to set the exit condition of your while-loop (progressStatus = 100) in order to make it terminate

Upvotes: 1

Vyacheslav
Vyacheslav

Reputation: 27221

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

Upvotes: 2

Related Questions