关于Android中ProgressDialog的几个问题

发布于 2024-11-01 13:22:06 字数 780 浏览 3 评论 0原文


1)FOA PD(ProgressDialog)只能从Activity创建,不是吗?如果确实不是,请提供一个有用的示例。

2)如果一个PD应该在单独的线程中创建,它是否可以被创建并显示它的线程是否根本不做任何事情?我的意思是这样的(假设 mProgressDialog 是该类的属性):

new Thread(){
      public void run(){
          mProgressDialog = ProgressDialog.show(appContext, 
                  appContext.getResources().getString(R.string.progress_wait), 
                  appContext.getResources().getString(R.string.progress_db_installing), 
                  true);

  }.start();

据我了解,线程在执行 run() 后立即死亡,因为没有什么可做的,所以 PD 不显示。它应该有一些处理代码或至少有一个具有一些可管理条件的空循环

3) 如果 PD 应该在主线程中创建,那么它应该仅在 OnCreate() 方法结束时创建,还是在 OnCreate() 方法中启动的某个调用/捕获(由某些侦听器)的方法主体中创建?
< br> 4) PD 本身在显示时不会挂起任何线程,不是吗?因此代码在 show() 方法之后继续执行。我的意思是 show() 本身不会挂起/暂停线程,因为我猜它会挂起/暂停线程。

1) FOA PD (ProgressDialog) can be created only from Activity, doesn't it? Please provide an useful example if it is really not.

2) If a PD should be created in separate thread could it be created and showed if it's thread doesn't do anything at all? I mean something like this (assuming mProgressDialog is a property of the class):

new Thread(){
      public void run(){
          mProgressDialog = ProgressDialog.show(appContext, 
                  appContext.getResources().getString(R.string.progress_wait), 
                  appContext.getResources().getString(R.string.progress_db_installing), 
                  true);

  }.start();

As I understand the thread dies immediately after executing run() cause there is nothing to do and so PD doesn't show. It should have some processing code or at least an empty cycle with some manageable condition

3) if PD should be created in the main thread should it be created only at the end of OnCreate() method or in the body of some method called/caught(by some Listener) started in OnCreate() method?

4) PD by itself doesn't suspend any thread while displaying, does it? So the code continues executing after the show() method . I mean the show() by itself doesn't suspend/pause the thread cause I guessed it does.

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

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

发布评论

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

评论(1

怎言笑 2024-11-08 13:22:06

1) 不确定它如何相关,除非能想出在 Activity 上下文之外创建 ProgressDialog 的理由;但我认为答案是“不”。

2) 不,您不能直接从后台线程创建对话框。你试过你的代码吗?它会因异常而死亡,并带有有用的回溯。请参阅有关如何回调 UI 线程以执行诸如显示对话框之类的操作的众多 SO 问题中的任何一个。

3)您可以在活动中的任何位置创建它;例如,通常在 AsyncTask,可以通过 onClick 回调触发。

4)没有。

1) Not sure how it's relevant unless you can come up with a reason to create a ProgressDialog outside of an Activity context; I think the answer is "no," though.

2) No, you can't create a dialog from a background thread directly. Have you tried your code? It'll die with an exception with a helpful traceback. See any one of a number of SO questions about how to call back to the UI thread to do things like show dialogs.

3) You can create it anywhere in your activity; for example, it's common to do this in onPreExecute() in an AsyncTask, which may be triggered from an onClick callback.

4) No.

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