android - 在非UI线程中显示ProgressDialog

标签 android multithreading progressdialog

这段代码来自一本书。非UI线程中似乎显示了一个“进度对话框”。

但据我所知,这仅在UI线程中是可能的。

我不知道为什么可以在非Ui线程中显示ProgressDialog。

public class BackWork3 extends Activity 
{
     TextView mResult;
     public void onCreate(Bundle savedInstanceState) 
     {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.backwork);
        mResult = (TextView)findViewById(R.id.result);
     }
     public void mOnClick(View v) 
     {
        int start = 0, end = 100;
        int result;
        WaitDlg dlg = new WaitDlg(BackWork3.this, "WaitTest", "Now calculating");
        dlg.start();
        result = Accumulate(start, end);
        mResult.setText("" + result);
        WaitDlg.stop(dlg);
     }
     int Accumulate(int start, int end) 
     {
         int sum = 0;
         for (int i = start; i <= end; i++) 
         {
             sum += i;
             try { Thread.sleep(20); } catch (InterruptedException e) {;}
         }
         return sum;
     }
}
class WaitDlg extends Thread 
{
    Context mContext;
    String mTitle;
    String mMsg;
    ProgressDialog mProgress;
    Looper mLoop;

    WaitDlg(Context context, String title, String msg) 
    {
        mContext = context;
        mTitle = title;
        mMsg = msg;

       setDaemon(true);
    }

    public void run() 
    {
       Looper.prepare();
       mProgress = new ProgressDialog(mContext);
       mProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
       mProgress.setTitle(mTitle);
       mProgress.setMessage(mMsg);
       mProgress.setCancelable(false);
       mProgress.show();

       mLoop = Looper.myLooper();
       Looper.loop();
    }


    static void stop(WaitDlg dlg) 
    {
       dlg.mProgress.dismiss();

       try { Thread.sleep(100); } catch (InterruptedException e) {;}

       dlg.mLoop.quit();
   }
}
//------------------------------------------------------------------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button  
android:id="@+id/start"
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:onClick="mOnClick"
android:text="Start"
/>
<TextView  
android:id="@+id/result"
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:textSize="40sp"
android:text="result"
/>
</LinearLayout>

最佳答案

该代码在创建处理程序的非UI线程中使用Looper,此循环程序成为对该处理程序的引用。这样,您可以在非UI线程中使用UI元素。

根据文档。

Looper.Prepare():

Initialize the current thread as a looper. This gives you a chance to create handlers that then reference this looper, before actually starting the loop.

希望这可以帮助

关于android - 在非UI线程中显示ProgressDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19166091/

相关文章:

android - 如何使用Lighting Color Filter使图像由暗变亮

android - 如何在 android studio 中访问 Google Cloud Save API?

android - 无法从 Android 10 上的 BroadcastReceiver 启动 Activity

android - ProgressBar在第二次打开时没有重置为 "0"

java - ProgressDialog Android AsyncTAsk

android - 在 RecyclerView 下面添加带按钮的 RelativeLayout

c++ - QWaitCondition,除了手动重置? (或者在 Qt Concurrent 之外创建 QFuture 对象?)

java - 高效的缓存同步

java - 进度对话框未出现

android - 如何在每次单击 WebView 中的链接时显示 ProgressDialog