在 Android 中使用线程显示进度对话框

发布于 2024-12-23 05:56:32 字数 1485 浏览 1 评论 0原文

这是一些代码:

public class ShowDialog extends Thread {
private static String mTitle="Please wait";
private static String mText="Loading...";
private Activity mActivity;
private ProgressDialog mDialog;

ShowDialog(Activity activity) {
    this(activity, mTitle, mText);
}
ShowDialog(Activity activity, String title) {
    this(activity, title, mText);
}
ShowDialog(Activity activity, String title, String text) {
    super();
    mText=text;
    mTitle=title;
    mActivity=activity;
    if (mDialog == null) {
        mDialog = new ProgressDialog(mActivity);
        mDialog.setTitle(mTitle);
        mDialog.setMessage(mText);
        mDialog.setIndeterminate(true);
        mDialog.setCancelable(true);
        mDialog.setOnCancelListener(
            new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface arg0) {
                    mDialog.dismiss();
                    interrupt();
                }
            });
    }
}

public void run() {
    mDialog.show();
    while(!isInterrupted())
    mDialog.dismiss();
    mDialog=null;
}

}

在我的主要活动中:

ShowDialog show = new ShowDialog(this, "Please wait!","Loading badly...");
    show.start();
    SystemClock.sleep(2000);
    show.interrupt();

我知道我可能会使用异步任务和所有的东西,但这不是我想要的。将 SystemClock.sleep 替换为需要一些时间的任何内容。这个想法是在 UI 线程中执行启动和中断之间的代码,并创建一个单独的线程来处理 ProgressDialog。 我的线程出了什么问题?

多谢!

Here is a few bit of code:

public class ShowDialog extends Thread {
private static String mTitle="Please wait";
private static String mText="Loading...";
private Activity mActivity;
private ProgressDialog mDialog;

ShowDialog(Activity activity) {
    this(activity, mTitle, mText);
}
ShowDialog(Activity activity, String title) {
    this(activity, title, mText);
}
ShowDialog(Activity activity, String title, String text) {
    super();
    mText=text;
    mTitle=title;
    mActivity=activity;
    if (mDialog == null) {
        mDialog = new ProgressDialog(mActivity);
        mDialog.setTitle(mTitle);
        mDialog.setMessage(mText);
        mDialog.setIndeterminate(true);
        mDialog.setCancelable(true);
        mDialog.setOnCancelListener(
            new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface arg0) {
                    mDialog.dismiss();
                    interrupt();
                }
            });
    }
}

public void run() {
    mDialog.show();
    while(!isInterrupted())
    mDialog.dismiss();
    mDialog=null;
}

}

And in my main activity:

ShowDialog show = new ShowDialog(this, "Please wait!","Loading badly...");
    show.start();
    SystemClock.sleep(2000);
    show.interrupt();

I know I might use an async task and all the stuff but that is not what I want. Replace the SystemClock.sleep by anything that takes some time. The idea is to execute the code between start and interrupt in the UI thread and make a seperate thread handling the ProgressDialog.
What's wrong with my thread ?

Thanks a lot!

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

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

发布评论

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

评论(1

始终不够 2024-12-30 05:56:32

要等待线程完成,您应该使用 Thread.join() 方法。但现在我发现您不想等待它完成,但您需要控制它何时完成。尽管如此,您还是希望避免以这种方式中断线程。

ShowDialog 类中,添加一个 dismiss() 方法,您可以从 main 调用该方法,而不是 interrupt().还要添加一个 boolean Dismiss = false 实例变量。在 dismiss() 中,添加 dismiss = true,然后添加 notify();。在 run() 中,用 while(!dismiss){wait()}< 替换常量 while() 循环(一直运行,效率很低) /代码>。您仍然需要添加同步块和异常处理,但这应该会让您有一个良好的开端。

这是一个通用 Java(非 Android)简化示例:

public static class ShowDialog extends Thread{

    protected boolean dismiss;

    public void dismiss(){
        dismiss = true;
        synchronized(this){
            notifyAll();
        }
    }

    @Override
    public void run(){
        System.out.println("Running...");
        // Show your dialog here.
        while(!dismiss){
            synchronized(this){
                try{
                    wait();
                }catch(InterruptedException ie){
                    ie.printStackTrace();
                }
            }
        }
        System.out.println("Quitting...");
        // Dismiss your dialog here.
    }

}

public static void main(String[] args) throws Exception{
    ShowDialog sd = new ShowDialog();
    sd.start();
    Thread.sleep(2000);
    sd.dismiss();
}

您提到您不想使用 AsyncTask,但我仍然会重新考虑。 (你反对的理由是什么?)

To wait for a Thread to complete, you should use the Thread.join() method. But now I see you're not wanting to wait for it to complete, but you need to control when it completes. Still, you want to avoid interrupting threads in this fashion.

In your ShowDialog class, add a dismiss() method that you can call from your main, instead of interrupt(). Also add a boolean dismiss = false instance variable. In dismiss(), add dismiss = true, then notify();. In run(), replace your constant while() loop (was running constantly, and very inefficient) with while(!dismiss){wait()}. You will still need to add synchronization blocks and exception handling, but this should get you off to a good start.

Here is a generic-Java (non-android) simplified example:

public static class ShowDialog extends Thread{

    protected boolean dismiss;

    public void dismiss(){
        dismiss = true;
        synchronized(this){
            notifyAll();
        }
    }

    @Override
    public void run(){
        System.out.println("Running...");
        // Show your dialog here.
        while(!dismiss){
            synchronized(this){
                try{
                    wait();
                }catch(InterruptedException ie){
                    ie.printStackTrace();
                }
            }
        }
        System.out.println("Quitting...");
        // Dismiss your dialog here.
    }

}

public static void main(String[] args) throws Exception{
    ShowDialog sd = new ShowDialog();
    sd.start();
    Thread.sleep(2000);
    sd.dismiss();
}

You mentioned you didn't want to use AsyncTask, but I would still reconsider. (What are your reasons against it?)

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