AlertDialog setmessage 在 Asynctask 内不起作用

2024-02-24

下面是我尝试通过 onProgressUpdate 方法在 Asynctask 中显示进度的代码,但它不会显示在警报对话框中。仅显示初始消息。

 class DownloadFileFromURL extends AsyncTask<String, String, String> {

        private AlertDialog.Builder alert;
        private int progress = 0;
        /**
         * Before starting background thread Show Progress Bar Dialog
         */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            alert = new AlertDialog.Builder(context);
            alert.setTitle("Downloading..");
            alert.setMessage("1");
            alert.setCancelable(false);
            alert.show();
        }

        /**
         * Downloading file in background thread
         */
        @Override
        protected String doInBackground(String... f_url) {
            int count;
            try {
                URL url = new URL(f_url[0]);
                URLConnection conection = url.openConnection();
                conection.connect();

                // this will be useful so that you can show a tipical 0-100%
                // progress bar
                int lenghtOfFile = conection.getContentLength();

                // download the file
                InputStream input = new BufferedInputStream(url.openStream(),
                        8192);

                // Output stream
                OutputStream output = new FileOutputStream(file);

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;
                    // publishing the progress....
                    // After this onProgressUpdate will be called
                    publishProgress("" + (int) ((total * 100) / lenghtOfFile));

                    // writing data to file
                    output.write(data, 0, count);
                }

                // flushing output
                output.flush();

                // closing streams
                output.close();
                input.close();

            } catch (Exception e) {
                Log.e("Error: ", e.getMessage());
            }

            return null;
        }

        /**
         * Updating progress bar
         */
        @Override
        protected void onProgressUpdate(String... progress) {
            Log.d("Myapp","progress :"+progress[0]);
            alert.setMessage(""+progress[0]);
        }

        /**
         * After completing background task Dismiss the progress dialog
         **/
        @Override
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after the file was downloaded
}
}

由于我还编写了一个日志来显示 onProgressUpdate 中的进度更新,因此打印了日志,但 onProgressUpdate 中的alert.setMessage 似乎没有将消息设置到我的警报对话框中。


根据你的代码,alert is an AlertDialog.Builder而不是一个AlertDialog本身。这引起了我的担忧,因为它可能不会改变的原因是你已经向构建器展示了,但没有给AlertDialog。所以我尝试了一个简单的代码:

public class MainActivity extends AppCompatActivity {

    private AlertDialog.Builder alert;
    private AlertDialog ad;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        alert = new AlertDialog.Builder(this);
        alert.setTitle("Downloading..");
        alert.setMessage("1");
        alert.setCancelable(false);
        ad = alert.show();


        Log.d("SAMPLE", "SET MESSAGE 2");
        alert.setMessage("2");

        Log.d("SAMPLE", "SET MESSAGE 3");
        ad.setMessage("3");
    }

}

起初,我只是使用alert.setMessage(这是AlertDialog.Builder),并且消息根本没有改变。但将其放入后AlertDialog然后设置消息AlertDialog例如,消息发生了变化。小心尝试一下这种方法。通过AlertDialog.Builder to an AlertDialog首先然后setMessage使用AlertDialog实例。

文档用于警报对话框 http://developer.android.com/reference/android/app/AlertDialog.html and AlertDialog.Builder http://developer.android.com/reference/android/app/AlertDialog.Builder.html.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AlertDialog setmessage 在 Asynctask 内不起作用 的相关文章

  • 您的应用中的 Google Analytics SDK

    我按照这里的说明进行操作 https developers google com analytics devguides collection android v3 https developers google com analytics
  • 即使我单击“运行”,Eclipse 也会运行调试模式

    Eclipse 总是在调试模式下启动我的应用程序 即使我单击常规的 运行 按钮 有任何想法吗 我发现我必须重新启动 Xoom 才能使其再次正常工作
  • 服务如何在后台运行 - Android

    今天的采访中我被问到了这个问题 什么是服务 我对此的回答是 Service 是 Android 的基本组件 它没有 UI 并且在后台运行 Service 是否在主线程上运行 不 那么它是如何在后台运行的呢 我心里一片空白 有人可以解释一下如
  • 将用户重定向到 iTunes 应用商店或 Google Play 商店?

    我正在寻找一种简单的解决方案来发布我的应用程序的一个链接 例如在 Facebook 上 如果用户使用移动设备访问它 它应该自动重定向到正确的应用程序商店 否则 用户应该被重定向到我的网站 iOS应用程序 http itunes apple
  • ndk-build error.opencv2/core/core.hpp:没有这样的文件或目录

    我在 Android 中使用 OpenCV Nonfree 模块时遇到问题 我读了这个教程https sites google com site wghsite technical notes sift surf opencv androi
  • 在 Android 市场中以编程方式检查我的应用程序版本

    目前 我正在启动时检查应用程序版本代码 并将其与我的服务器上的最新版本代码进行匹配 并根据此匹配 我发送用户从 Android 市场获取最新更新 它运行良好 但我的问题是我必须手动更改服务器上的最新版本代码 并且我不知道新版本何时发布APK
  • 突出显示列表视图项目

    我需要在触摸列表视图项目时突出显示它并保持突出显示状态 我尝试了我发现的一切 但没有任何效果 这是我的代码 这是列表视图
  • 新安装的Eclipse和Android SDK。无法让模拟器工作。挂在时钟屏幕上

    我对开发是全新的 我已经安装了 Eclipse 和 Andoid SDK 但是 我无法让模拟器工作 我已经尝试过示例记事本代码和 Hello Android 教程代码 每次我尝试运行任一应用程序时 它都会挂在时钟屏幕上 屏幕上还显示正在充电
  • Android Studio APK META-INF/BCKEY.DSA 中复制的重复文件

    我的代码构建得很好 但是当我尝试在调试中运行它时 出现以下错误 Error Execution failed for task app transformResourcesWithMergeJavaResForDebug com andro
  • 使用 START_STICKY 启动时服务进程被终止后的 onStartCommand

    我一直在阅读 Android 文档 我想知道是否有人可以阐明当以 START STICKY 启动的服务的进程被终止时服务实例会发生什么情况 我假设本地状态数据 实例变量 也丢失了 Android 在重新创建服务时是否会采取任何措施来帮助重新
  • 在 Android 中使用 AES 加密的最佳实践是什么?

    我为什么问这个问题 我知道人们对 AES 加密存在很多疑问 即使对于 Android 也是如此 如果您在网络上搜索 会发现很多代码片段 但在每个页面上 在每个 Stack Overflow 问题中 我都发现了另一个具有重大差异的实现 所以我
  • 何时调用 glMatrixMode()

    我所关注的大多数 Android OpenGL ES 教程都有其 onSurfaceChanged 函数 如下所示 public void onSurfaceChanged GL10 gl int width int height gl g
  • 通过配置更改保留 CoroutineScope 的干净方法,无需 ViewModel

    我知道建议是在我们的 Activity 中使用 ViewModel 这样我们就可以使用它viewModelScope 由于 ViewModel 的寿命比 Activity 的寿命长 因此我们不必取消以下作业activity onDestro
  • Fresco:滚动 RecyclerView 后图像消失

    我有一个 Horizo ntal RecyclerView 每个项目都有一个使用 Facebook Fresco 图像库加载到其中的图像 然而 虽然正确的图像最初是在屏幕上滚动一点时加载的 但当 RecyclerView 进一步滚动时 它就
  • 如何在Room的数据库迁移中正确添加索引?

    我在迁移 Room 数据库时遇到问题 在更新的数据库中 我必须将一个字段从整数更改为双精度值 我读到它并不像听起来那么容易 为了做到这一点 我必须使用这个更改后的属性创建新的临时表 复制前一个表中的所有值 删除旧的值 最后重命名临时表 我的
  • 活动中列表视图中的粘滞行

    我的列表视图中只有一行应该是粘性的 而不是粘性标题中带有字母的部分或部分 我真的很感激任何关于列表视图在活动中粘性一行而不是片段的帮助 我该怎么做 我真的很感谢任何帮助 提前致谢 使用如下代码 class MyAsyncTask exten
  • Android:选择 EditField 上焦点上的所有文本

    我试图让 Android 在获得焦点时选择 EditText 字段中的所有文本 我在布局中使用此属性 在两个字段上 android selectAllOnFocus true 我不确定这是否相关 但为了将光标移动到第一个可编辑字段 前面 还
  • 如何以编程方式检测android中可用的底部软导航栏?

    我试图通过 android 程序确定软导航栏 我没有找到直接的方法来确定 有没有办法找到导航栏的可用性 软导航栏图像在这里 以下方法对我有用并在许多设备上进行了测试 public boolean hasNavBar Resources re
  • 在两个片段之间拖放视图

    我目前正在尝试在两个片段之间实现拖放 我已经将它们添加到我的活动中 如下所示 FragmentManager fm getFragmentManager FragmentTransaction ft fm beginTransaction
  • 将主题应用到 v7 支持操作栏

    我正在使用support v7库来实现ActionBar在我的应用程序中 我的styles xml file

随机推荐