开发者

Incrementing progressbar during a delayed call

开发者 https://www.devze.com 2023-03-14 13:50 出处:网络
I have a delayed call which looks like this: handler.postDelayed(new Runnable() { public void run() { newDeviceBluetooth();

I have a delayed call which looks like this:

handler.postDelayed(new Runnable() { 
            public void run() {
                newDeviceBluetooth();
            //increment progressbar each thousand millis passed
            }
       },15000);

However I've got a fully functional progressbar that I want to increase with each second passed by the delay. So what is the approach on this problem, is it a way to implement this?

Appreciate the time you take to answer and help me


So after I had a look at what was possible from the answers I wrote my own class that implements runnable. Is this a possible way to implement this? Think I kinda got it wrong if my gut feeling isn't messing around.

    public void run() {
    while(counter <= 15){
        if(counter < 15){
            handler.开发者_运维百科postDelayed(new Runnable() {
                @Override
                public void run() {
                    counter++;
                }
            }, 1000);
        }
        else{
            sb.newDeviceBluetooth();
        }
    }

}


Is is possible for you to use AsyncTask instead? Then you can call publishProgress() and update the UI in onProgressUpdate()

Maybe it's not the best solution since it would require another thread.


But you can make your Runnable fire every second. You can do that by having the run() method do a postDelayed() to your handler.

Make your own class that extends runnable, and use a member variable for a counter. In the normal case update the UI, and call newDeviceBluetooth() on the 15th time.


Found an answer to the problem. There's a sample code in the api demos under App/dialog/Progress dialog

So the code below needs to be modified but it displays a delay of the progressbar.

        mProgressHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (mProgress >= MAX_PROGRESS) {
                mProgressDialog.dismiss();
            } else {
                mProgress++;
                mProgressDialog.incrementProgressBy(1);
                mProgressHandler.sendEmptyMessageDelayed(0, 100);
            }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号