簡體   English   中英

Asynctask 出一個 UI 線程

[英]Asynctask out a UI thread

我想給 AsyncTask class 打上烙印,以便在后台更新調用者線程的進度,但使用 UI 線程的調用者線程。=。 我已經嘗試過該代碼,但 publishProgress(i) 行似乎沒有效果。 有人可以告訴我如何修復它(或者我還可以使用其他什么 class )。 提前感謝=)

 public class MainUI extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Thread t=new Thread(){
                    boolean exit=false;
                    public void run(){
                        Looper.prepare();
                         new DownloadFilesTask().execute();

                         while (!exit){
                             try {
                                Thread.sleep(600);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                         }

                         Log.d("","Exit thread");

                        Looper.loop();
                    }

                    public void exit(){
                        exit=true;
                    }

                    class DownloadFilesTask extends AsyncTask<Void, Long, Long> {
                        protected Long doInBackground(Void... urls) {
                            long i=0;
                            for (i=0;i<20;i++){
                                 Log.d("",i+" ");
                                try {
                                    Thread.sleep(500);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                publishProgress(i);
                                }
                            return i;
                        }
                        protected void onProgressUpdate(Long... progress) {
                            Log.d("Test",progress[0]+"");
                                        }

                                        protected void onPostExecute(Long result) {
                                            exit();
                                        }
                                    }


                                };
                                t.start();


                            }

                        });
                    }





}

創建線程來啟動 ASyncTask 是沒有用的,不應該這樣做。 您應該閱讀 android 開發人員網站上的無痛線程文檔

http://android-developers.blogspot.com/2009/05/painless-threading.html

來自 Android 文檔:

要使此 class 正常工作,必須遵循一些線程規則:

任務實例必須在 UI 線程上創建。

execute(Params...) must be invoked on the UI thread.
Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually.
The task can be executed only once (an exception will be thrown if a second execution is attempted.)

所以你不能在 UI 線程之外創建它。 改用 Task 並將其包裝在 ThreadPoolExecutor object 中。 請注意,在使用以下之一更新 UI 時,您需要使其成為線程安全的:

Activity.runOnUiThread(Runnable)
View.post(Runnable)
View.postDelayed(Runnable, long)

但是再一次,Asynctask 沒用,我不推薦它。

問候

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM