Asynctask 输出 UI 线程

发布于 2024-11-19 06:13:31 字数 2554 浏览 1 评论 0原文

我想为 AsyncTask 类打上品牌,以便在后台执行一些操作,以进度更新调用者线程,但使用 UI 线程的调用者线程 != 。 我已经尝试过该代码,但publishProgress(i)行似乎没有效果。 有人可以告诉我如何修复它(或者我可以使用其他什么类)。 预先感谢 =)

 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();


                            }

                        });
                    }





}

I'd like to brand the AsyncTask class for doing something in background updating the caller thread with the progress but with the caller thread != of UI thread.
I've tried that code but the line publishProgress(i) seems have not effect.
Can someone tell me how can I fix it (or what other class can I use else).
Thanks in advance =)

 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();


                            }

                        });
                    }





}

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

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

发布评论

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

评论(2

纸短情长 2024-11-26 06:13:31

创建线程来启动 ASyncTask 是没有用的,不应该这样做。
您应该阅读 android 开发人员网站上的无痛线程文档

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

Creating a thread to start an ASyncTask is useless and should not be done.
You should read the painless threading document on the android developer site

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

淡淡绿茶香 2024-11-26 06:13:31

来自 Android 文档:

要使此类正常工作,必须遵循一些线程规则:

任务实例必须在 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 对象中。请注意,在使用其中之一更新 UI 时,您需要使其成为线程安全的:

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

但再次强调,Asynctask 毫无用处,我不推荐它。

问候

From the Android Doc :

There are a few threading rules that must be followed for this class to work properly:

The task instance must be created on the UI thread.

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.)

So you can NOT create it outside of the UI Thread. Use Task instead and wrap it in a ThreadPoolExecutor object. Be aware that you need to make it thread-safe when updating the UI by using one of those :

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

But once again, Asynctask is useless and I do NOT recommend it.

Regards

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