简体   繁体   English

android:显示进度对话框

[英]android: showing a progress dialog

I have looked at the Android API and other posts here on stackoverflow, but have not been able to figure this out. 我已经在stackoverflow上查看了Android API和其他文章,但无法弄清楚这一点。

My app downloads files to the sd card. 我的应用程序将文件下载到SD卡。 I would like to pop a "loading..." dialog while the file is downloading and then have it disappear when the download is finished. 我想在下载文件时弹出一个“正在加载...”对话框,然后让它在下载完成后消失。 This is what i came up with using the android API: 这是我使用android API想到的:

ProgressDialog pd = ProgressDialog.show(this,"","Loading. Please wait...",true);

//download file

pd.cancel();

however the dialog doesn't actually show. 但是该对话框实际上并未显示。 when i debug it, it claims that it is showing, but it is obviously not on the screen. 当我调试它时,它声称它正在显示,但是显然它不在屏幕上。

what can i do? 我能做什么?

You have to run the download code into a separated Thread. 您必须将下载代码运行到单独的线程中。 The easy way is to use AsyncTask 简单的方法是使用AsyncTask

Look also this article on how to use it 还要看这篇关于如何使用它的文章

I have a somewhat similar scenario, more of a level loading pause while stuff is being setup for a phase of a game. 我有一个类似的场景,在为游戏的某个阶段设置内容时,更多的是关卡加载暂停。 I launch the dialog, start level loading in a thread which is passed the context and call this at the end of the thread: 我启动对话框,在传递上下文的线程中开始级别加载,并在线程末尾调用此函数:

mContext.runOnUiThread(new Runnable(){
    public void run() {
        mContext.mProgressDialog.dismiss();
    }
});

I find the runOnUiThread() method extremely useful. 我发现runOnUiThread()方法非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM