0

This Code means that if I Click a button, a progress bar start 0 to 100%. And I want to make the progress bar reset, when I click a button before the progress bar reaches 100%.

Here is a part of my code.

This code is button listener.

public void Cal_btn(View v) {

    Message msg;
    switch (v.getId()) {
        case R.id.Square:
            if (Number.getText().toString().length() == 0) {
                Toast.makeText(getApplicationContext(), "숫자를 입력하세요.", Toast.LENGTH_LONG).show();
            } else {
                pThread = new ProThread(pHandler);
                pThread.setDaemon(true);
                pThread.start();

                Cal_Result.setVisibility(View.GONE);
                progress.setVisibility(View.VISIBLE);
                msg = new Message();
                msg.what = 1;
                msg.arg1 = Integer.parseInt(Number.getText().toString());
                mThread.mBackHandler.sendMessage(msg);
            }
            break;
    }
}

And this code is handler.

Handler pHandler = new Handler(){

    public void handleMessage(Message msg){
        if(msg.what == 3){
            if(msg.arg1 == 100){
                Cal_Result.setVisibility(View.VISIBLE);
                progress.setVisibility(View.GONE);
            }else{
                progress.setProgress(msg.arg1);
            }
        }
    }
};

And this code is Thread run code.

class ProThread extends Thread{
    int proNum = 0;
    Handler pHandler;
    ProThread(Handler handler){
        pHandler = handler;
    }

    public void run(){
        while(proNum != 100) {
            proNum++;
            Message msg = new Message();
            msg.what = 3;
            msg.arg1 = proNum;
            pHandler.sendMessage(msg);
            try {
                Thread.sleep(10);
            }catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
JorgeAmVF
  • 1,660
  • 3
  • 21
  • 32
오영택
  • 25
  • 2
  • 8
  • So what is the problem? First, you are starting ProThread in MainThread. It may block the MainThread. You need to check the concept and implement of "Handler/MessageQueue" again. – yoonhok Jan 30 '18 at 08:10
  • Try to disable button click once its clicks and reactive button click when progress bar reach 100%. – Fahad Ejaz Butt Jan 30 '18 at 08:11
  • @yoonhok When I click a button before the progress bar reaches 100%, another progress bar start too. So two progress bars overlap, and first started progress bar do not stop. – 오영택 Jan 30 '18 at 08:28
  • @FahadEjazButt that is not way I want... – 오영택 Jan 30 '18 at 08:29
  • I already told you, you are starting the ProThread in MainThread directly. You should implement the runnable object and forward it in the handler like: Handler mHandler = new Handler(); mHandler.post(mRunnable); – yoonhok Jan 30 '18 at 09:11
  • new ProThread will always create new thread so you have either use Runnable interface or destroy previous thread before creating new thread. – Pawan Chaurasiya Jan 30 '18 at 09:20

1 Answers1

0

Add a boolean member variable as a marker for "ProgressBar" started or not.

boolean isProgressBarRun;

When the button is clicked, change the status of this variable. And if the button is clicked at first time, send a message. And when you handle the message, re-send the messages every 10 ms in your "public void handleMessage(Message msg)" method.

And your onClick method can be written like below:

boolean isProgressBarRun = false;

...

public void Cal_btn(View v) {

    Message msg;
    switch (v.getId()) {
        case R.id.Square:
            if (Number.getText().toString().length() == 0) {
                Toast.makeText(getApplicationContext(), "숫자를 입력하세요.", Toast.LENGTH_LONG).show();
            } else {
                if (isProgressBarRun) {
                    isProgressBarRun = false;
                    msg = new Message();
                    msg.what = 4; // to stop the progress bar
                    mThread.mBackHandler.sendMessage(msg);
                    msg.what = 3;
                    msg.arg1 = Integer.parseInt(Number.getText().toString());
                    mThread.mBackHandler.sendMessage(msg);
                } else {
                    isProgressBarRun = true;
                    Cal_Result.setVisibility(View.GONE);
                    progress.setVisibility(View.VISIBLE);
                    msg = new Message();
                    msg.what = 1;
                    msg.arg1 = Integer.parseInt(Number.getText().toString());
                    mThread.mBackHandler.sendMessage(msg);
                }
            }
            break;
    }
}

Your handler can be changed like below:

Handler pHandler = new Handler(){
    public void handleMessage(Message msg){
        if(msg.what == 4){
            progress.setVisibility(View.GONE);
        } else {
            progress.setProgress(msg.arg1);
            Message message = new Message();
            message .what = 3;
            message .arg1 = msg.arg1 + 1;
            pHandler.sendMessageDelayed(message, 10);
        }
    }
};

In summary, you don't need to implement Thread. Upper codes are incorrect, just see the concept please.

yoonhok
  • 2,575
  • 2
  • 30
  • 58
  • I did not use your code, but I could understand what is the problem on my code. And I solved the problem well. Thank you for Teaching thread concept. – 오영택 Jan 31 '18 at 01:38