java - Asynctask 输出 UI 线程

标签 java android android-asynctask

我想为 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();


                            }

                        });
                    }





}

最佳答案

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

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

关于java - Asynctask 输出 UI 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6635511/

相关文章:

android - Kotlin 和安卓: is it necessary to cancel previously setup callbacks?

Android ProgressDialog 不与 AsyncTask 一起工作

java - EntityManager 能知道它死了吗?

java - 我在 animate() 中收到一个错误,它是 "error: non-static method animate() cannot be referenced from a static context"

java - 尝试编译 Telegram 源代码时找不到文件

android - 在 onCreate 中使用 PersistableBundle 时,它​​显示一个空白页面

java - Android:使用AsyncTask类的回调方法来更改Activity

java - GCM 注册阻塞 AsyncTask 直到发生超时

java - Classx 不是抽象的,不会重写 Interfacex 中的抽象方法 methodx(java.lang.Object)

java - 更新ADT后Eclipse无法导出apk文件?