【问题标题】:Updating a dialog with setMessage() does not work使用 setMessage() 更新对话框不起作用
【发布时间】:2018-05-21 16:18:06
【问题描述】:

在您将此问题视为重复问题之前,只需知道我看到了这些:

AlertDialog setmessage not working inside Asynctask

ProgressDialog does not want to update the message

Android: Progress Dialog change ProgressDialog.setMessage() while loading

Changing Progress Dialog Message While Running

但没有运气。

我正在尝试更新 ProgressDialog 中显示的消息。是的。就这么简单。

现在我的代码如下所示:

private BroadcastReceiver createMapReceiver(MapEntry entry) {
        return new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

            ...

             dialogMessage = getString(R.string.maps_download_extracting)
                   .concat("\n\n")
                   .concat(getString(R.string.maps_download_suffix));

             Runnable changeMessage = () -> {
                    downloadingDialog.setMessage(dialogMessage);
                    downloadingDialog.show();
             };

             runOnUiThread(changeMessage);

             ...

        }
    };
}

但是消息没有更新。其他一切都按预期工作。我错过了什么?

【问题讨论】:

  • 你不需要 runOnUiThreadonReceive 已经在 mainThread 上调用过。 onReceive 是否被调用?
  • 是的。它是。这就是我所说的“其他一切都按预期工作”的意思:p。我需要 runOnUiThread 因为 onReceive 没有在 UI 线程上运行。
  • 它如何不在 UI 线程上运行?在onReceive 之前downloadingDialog 的状态是什么?正在显示吗?
  • 正在显示下载对话框。关键是,无论是否在 UI 线程上运行,它都不会更新消息。两种方法我都试过了。
  • 你是对的。 onreceive() 在 UI 线程上运行。谢谢 :) 不过还是不行 :(

标签: java android


【解决方案1】:

我找到了解决办法。

不过,我仍然不知道为什么它不起作用。

奇怪的是,我只是在接收器内部调用另一个函数,它会继续这个过程。

像这样:

    private BroadcastReceiver createMapReceiver(MapEntry entry) {
        return new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                downloadingDialog.dismiss();
                unregisterReceiver(this);

                ...

                receiveDownloadedFile(new File(completeFilePath), entry);
            }
        };
    }



private void receiveDownloadedFile(File downloadedFile, MapEntry entry) {

   downloadingDialog.setMessage("TEST MESSAGE");

   ...

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-29
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多