在延迟通话期间增加进度条

发布于 2024-11-16 19:40:57 字数 795 浏览 7 评论 0原文

我有一个延迟的呼叫,如下所示:

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

但是,我有一个功能齐全的进度条,我希望随着延迟的每一秒而增加。 那么解决这个问题的方法是什么,有没有一种实现的方法呢?

感谢您花时间回答和帮助我


所以在我查看了答案中可能的内容之后,我编写了自己的实现可运行的类。这是实现这一点的可能方法吗?如果我的直觉没有乱七八糟的话,我想我有点错了。

    public void run() {
    while(counter <= 15){
        if(counter < 15){
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    counter++;
                }
            }, 1000);
        }
        else{
            sb.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();
        }
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光与爱终年不遇 2024-11-23 19:40:57

您可以使用 AsyncTask 代替吗?然后你可以调用publishProgress()并在onProgressUpdate()中更新UI。

也许这不是最好的解决方案,因为它需要另一个线程。


但你可以让你的 Runnable 每秒都燃烧。您可以通过让 run() 方法对处理程序执行 postDelayed() 来做到这一点。

创建您自己的扩展可运行的类,并使用成员变量作为计数器。
正常情况下更新UI,并在第15次调用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.

椒妓 2024-11-23 19:40:57

找到了问题的答案。 App/dialog/Progress 对话框下的 api demo 中有示例代码,

所以下面的代码需要修改,但它显示了进度条的延迟。

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

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);
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文