ProgressBar running faster after each onclick in android

Ashokprakash

I am Stuck here with this application in android. In my application i am trying to implement a progressbar which shows timer for certain seconds. When the Button is clicked the timer should refresh and again start from 0 in progressBar. For this I am using Thread.

The Problem is, When I Click the button the Thread calls the timer function and each time the thread is getting faster and faster. I couldn't resolve it and not having any idea what is going in background. This is my code for Timerfunction

public void setTimer()
    {
        prog=0;
        progress.setProgress(prog);
        if(flag){
        t= new Thread(new Runnable(){
            public void run()
            {
              while(prog<100)
              {
                prog+=1;
                handle.post(new Runnable(){
                    public void run()
                    {
                        progress.setProgress(prog);
                        if(prog==progress.getMax()&& flag){
                         call_fun();
                        }
                    }
                });
                try
                {
                  Thread.sleep(time);
                }
                catch(InterruptedException e)
                {
                    Log.i("Error", null);
                }
              }
            }
        });
        t.start(); 
        }
    }

I called this function in another function called RandomGeneration. If the button is clicked the randomgeneration is called and the set timer is activated everytime. But the progressbar is running faster after every click. It is constantly running in the same specific time. For example if it runs for 3 seconds in the first click, its running 2 seconds in the second click and getting faster considerably.

Can anyone please try to find what is happening in this code. Thanks in advance..!!

Chris Margonis

From what I see a new Thread is being created everytime you click the button. Maybe try to check if t is already running and if so update it's logic to set progress to 0? Also, what does if(flag) do?

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Array appending after each onclick and loop in javascript

분류에서Dev

Android Binder thread is running after unbind operation

분류에서Dev

Running multiple command line after each other linux python

분류에서Dev

Android에서 각 onclick 후 ProgressBar가 더 빠르게 실행됩니다.

분류에서Dev

ProgressBar does not reset after audio is finished done

분류에서Dev

How to capitalize each letter after a special letter in android?

분류에서Dev

원형 ProgressBar 배경 : Android Xamarin

분류에서Dev

Android 변경 ProgressBar 두께

분류에서Dev

How can I use progressbar in Android

분류에서Dev

Progressbar Android 스타일링

분류에서Dev

(XML) android : onClick?

분류에서Dev

Android onClick 작업

분류에서Dev

Android Map OnClick Tracker

분류에서Dev

Android - RecyclerView onClick not work

분류에서Dev

progressBar를 Android 함수로 설정

분류에서Dev

Android ProgressBar 타이머 카운트 다운

분류에서Dev

Xamarin Android 채우기 listview async with progressbar

분류에서Dev

jQuery each running multiple times on same element

분류에서Dev

Android의 Onclick 기능

분류에서Dev

Android : Actionbar 항목 onclick

분류에서Dev

Android Wear gridviewpager onClick Listener

분류에서Dev

Android Recyclerview Multiple onclick items

분류에서Dev

Android onClick from another class

분류에서Dev

Android onClick checkbox not checked in listview

분류에서Dev

onClick loading new page before running my function

분류에서Dev

Keep screen running after starting it with an alias and running some commands

분류에서Dev

How can my processor be running faster than it's base speed but only be utilizing < 30 %?

분류에서Dev

Android app running 3 times

분류에서Dev

Android check only desktop is running

Related 관련 기사

뜨겁다태그

보관