在 Android 中使用线程显示进度对话框
这是一些代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要等待线程完成,您应该使用 Thread.join() 方法。但现在我发现您不想等待它完成,但您需要控制它何时完成。尽管如此,您还是希望避免以这种方式中断线程。
在
ShowDialog
类中,添加一个dismiss()
方法,您可以从main
调用该方法,而不是interrupt().还要添加一个
boolean Dismiss = false
实例变量。在dismiss()
中,添加dismiss = true
,然后添加notify();
。在run()
中,用while(!dismiss){wait()}< 替换常量
while()
循环(一直运行,效率很低) /代码>。您仍然需要添加同步块和异常处理,但这应该会让您有一个良好的开端。这是一个通用 Java(非 Android)简化示例:
您提到您不想使用
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 adismiss()
method that you can call from yourmain
, instead ofinterrupt()
. Also add aboolean dismiss = false
instance variable. Indismiss()
, adddismiss = true
, thennotify();
. Inrun()
, replace your constantwhile()
loop (was running constantly, and very inefficient) withwhile(!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:
You mentioned you didn't want to use
AsyncTask
, but I would still reconsider. (What are your reasons against it?)