无法让 ProgressDialog 在线程中显示

发布于 2024-10-31 04:54:30 字数 380 浏览 8 评论 0原文

在尝试让它发挥作用的过程中,我见过各种各样的失败。我有一个通过活动启动的线程。线程需要创建/显示进度对话框并关闭它们。

当我尝试直接显示 ProgressDialog 时,出现错误,表明我的 Looper 未准备好。我用 Looper 查找了一下,它是一个实现的。但是,我必须调用 Looper.loop 才能显示进度对话框。它出现后,应用程序冻结在该点上,永远不会继续执行 Looper.loop 调用。

我无法让它工作,所以寻找一种使用 HandlerThread 和 Handler 的全新方法。我创建一个 HandlerThread 并启动它。我从线程中获取循环器并用它创建一个处理程序。我的 ProgressDialog 或 Toasts 根本不会显示。

有没有更简单的方法来做到这一点?

Well I've seen a wide variety of failures while trying to get this to work. I have a thread that is started via an Activity. The thread needs to create/display progress dialogs and dismiss them.

When I tried to directly display the ProgressDialog I got an error that my Looper wasn't prepared. I looked up with a Looper was an implemented it. However, I had to call Looper.loop for the progress dialog to show up. After it showed up the application froze on that point never to continue past the Looper.loop call.

I couldn't get it to work so looked for a whole new way using a HandlerThread and a Handler. I create a HandlerThread and start it. I get the looper from the thread and create a Handler with it. My ProgressDialog or Toasts won't show up at all.

Is there an easier way to go about doing this?

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

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

发布评论

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

评论(3

血之狂魔 2024-11-07 04:54:30

您可以设置一个

private Handler stopProgressHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {         
        setProgressBarIndeterminateVisibility(false);
    }
};
private Handler startProgressHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {         
        setProgressBarIndeterminateVisibility(true);
    }
};

,以便在启动线程时启动进度条,并在线程完成后停止进度条。

 public void closeProgressbar(){
    //bluetoothconnector.onDestroy();
    stopProgressHandler.sendEmptyMessage(0);
 }
public void openProgressbar(){
    //bluetoothconnector.onDestroy();
    startProgressHandler.sendEmptyMessage(0);
 }

这将有助于调用 progressbar 来启动和停止。这将是解决方案之一。

U can have an

private Handler stopProgressHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {         
        setProgressBarIndeterminateVisibility(false);
    }
};
private Handler startProgressHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {         
        setProgressBarIndeterminateVisibility(true);
    }
};

so that when u start the thread start the progressbar and after thread is completed u can stop the progressbar.

 public void closeProgressbar(){
    //bluetoothconnector.onDestroy();
    stopProgressHandler.sendEmptyMessage(0);
 }
public void openProgressbar(){
    //bluetoothconnector.onDestroy();
    startProgressHandler.sendEmptyMessage(0);
 }

This will help to call the progressbar to start and stop.. This will be one of the solution..

半枫 2024-11-07 04:54:30

不确定 ProgressDialog,但据我所知,Android 中所有与 UI 相关的内容都需要在 UI Thread 中更新。实际上有一个简单的帮助器类用于实现异步任务:http://developer.android。 com/reference/android/os/AsyncTask.html

或者,您可以创建一个处理程序(位于 UI 线程上)并使用它创建对话框:

Handler uiHandler;
//Activity onCreate
onCreate(...){
    uiHandler = new Handler();
}

// Somewhere in your other thread, 
uiHandler.postRunnable(new Runnable(){
    @Override
    public void run(){
       // Create or update dialog
       ...
    }
});

Not sure about ProgressDialog, but all UI related stuff in Android, as far as I know, required to be updated in UI Thread. There's actually an easy helper class for implementing async task: http://developer.android.com/reference/android/os/AsyncTask.html

Alternatively, you can create a Handler (which would be on UI Thread) and create the dialog using that:

Handler uiHandler;
//Activity onCreate
onCreate(...){
    uiHandler = new Handler();
}

// Somewhere in your other thread, 
uiHandler.postRunnable(new Runnable(){
    @Override
    public void run(){
       // Create or update dialog
       ...
    }
});
飘过的浮云 2024-11-07 04:54:30

最后一个答案是错误的......

它应该是:

setProgressBarIndependentVisibility(Boolean.TRUE | Boolean.FALSE);

The last answer is wrong....

it should be:

setProgressBarIndeterminateVisibility(Boolean.TRUE | Boolean.FALSE);

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