传递消息从主题到更新的用户界面 [英] Pass a Message From Thread to Update UI

查看:100
本文介绍了传递消息从主题到更新的用户界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

香港专业教育学院创建了一个新的线程文件浏览器。线程读取目录的内容。我想要做的是更新UI线程绘制的文件和文件夹的图形重新presentation。我知道我不能从一个新的线程更新UI,所以我想要做的是:

而通过目录中的文件和文件夹的文件扫描线程迭代传递一个文件路径字符串返回到UI线程。在UI线程的处理程序然后绘制传递回该文件的图形重新presentation。

 公共类NEW_PROJECT扩展活动实现Runnable {

    私人处理程序处理程序=新的处理程序(){
        @覆盖
        公共无效的handleMessage(信息MSG){
            Log.d(新话题,Proccess完成。);
            意向意图=新的意图();
            的setResult(RESULT_OK,意图);
            完();
        }
    };


    公共无效fileScanner(){
        //如果回报(XMLEFunctions.canReadExternal(这一点)!);
        PD = ProgressDialog.show(这一点,阅读指南。
                请等待......,真,假);

        Log.d(新话题,被称为);
        线程线程=新主题(本);
        thread.start();
    }


    公共无效的run(){
        Log.d(新话题,阅读文件);
        的GetFiles();
        handler.sendEmptyMessage(0);
    }


    公共无效的GetFiles(){
        的for(int i = 0; I< = allFiles.length-1;我++){
            //我想传递的文件路径返回TU处理程序在用户界面
            //所以可以得出。
            ** passFilePathBackToBeDrawn(allFiles [I]的ToString()); **
        }
    }

}
 

解决方案

查看AsyncTask的用于这种东西。这真是更优雅比滚动自己的处理和传递信息的来回。

http://developer.android.com/reference/android/os/ AsyncTask.html

Ive created a new thread for a file browser. The thread reads the contents of a directory. What I want to do is update the UI thread to draw a graphical representation of the files and folders. I know I can't update the UI from within a new thread so what I want to do is:

whilst the file scanning thread iterates through a directories files and folders pass a file path string back to the UI thread. The handler in the UI thread then draws the graphical representation of the file passed back.

public class New_Project extends Activity implements Runnable {

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Log.d("New Thread","Proccess Complete.");
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
        }
    };


    public void fileScanner(){
        //if (!XMLEFunctions.canReadExternal(this)) return;
        pd = ProgressDialog.show(this, "Reading Directory.",
                "Please Wait...", true, false);

        Log.d("New Thread","Called");
        Thread thread = new Thread(this);
        thread.start();
    }


    public void run() {
        Log.d("New Thread","Reading Files");
        getFiles();
        handler.sendEmptyMessage(0);
    }


    public void getFiles() {
        for (int i=0;i<=allFiles.length-1;i++){
            //I WANT TO PASS THE FILE PATH BACK TU A HANDLER IN THE UI
            //SO IT CAN BE DRAWN.
            **passFilePathBackToBeDrawn(allFiles[i].toString());**
        } 
    }

}

解决方案

Check out AsyncTask for this kind of stuff. It's really much more elegant than rolling your own handler and passing messages back and forth.

http://developer.android.com/reference/android/os/AsyncTask.html

这篇关于传递消息从主题到更新的用户界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆