机器人:从另一个类另一个线程更新界面 [英] android: update UI from another thread in another class

查看:105
本文介绍了机器人:从另一个类另一个线程更新界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该方案是
我有两个线程和UI线程。在登录按钮按下时,UI线程创建它创建了一个套接字,并运行,直到套接字连接的ClientThread,每当收到消息我使用一个处理程序发布消息,称为ProcessDataThread另一个线程,现在从服务器,我需要接受一些消息从ProcessDataThread更新UI相关的东西,我四处搜寻了很多,我发现这两种方式runonUiThread功能,我想只能从活动类,这是无用和AsyncTask的方法,我不知道如何将活动范围内传递到运行...

下面是code

在MainActivity上的登录按钮被点击时执行的code

 公共无效onLoginClick(查看视图)
{
    global_constants.clientObject =新ClientThread();
    global_constants.clientThread =新主题(global_constants.clientObject);
    global_constants.clientThread.start();
}

的code在ClientThread run方法

 公共类ClientThread实现Runnable {
.......
    @覆盖
    公共无效的run(){
    ......
        而(Thread.currentThread()isInterrupted()及!&放大器;!(CloseThread))
        {
            字节[]的buff;
            ....
            global_constants.updateConversationHandler.post(新ProcessDataThread(BUFF));
        }
    }
}

的方法code在ProcessDataThread解析出传入的数据和填充后

 公共类ProcessDataThread实现Runnable {
.........
    无效ProcessLoginFailedPacket(字节[] BUFF)
    {
        //如何从这里调用UI线程更新一些UI元素?
    }
}

我存储在一个全局变量的活动环境,然后就是这么做的,但我不知道它是否会更安全与否

 ((活动)global_constants.MainContext).runOnUiThread(新的Runnable(){
    公共无效的run()
    {
        TextView的txtErr =(TextView中)((活动)global_constants.MainContext).findViewById(R.id.errMsg);
        txtErr.setVisibility(0);
        txtErr.setText(原因);
    }
});


解决方案

您可以发布一个可运行由它来完成的UI操作主线程如下,

 公共类utils的{    公共静态无效runOnUiThread(Runnable接口可运行){
        最后的处理程序UIHandler =新的处理程序(Looper.getMainLooper());
        UIHandler。员额(可运行);
    }
}Utils.runOnUiThread(新的Runnable(){
     @覆盖
     公共无效的run(){
         // UI相关的选择更新code。
     }
});

The scenario is I have two threads and a UI thread. The UI thread when clicked on login button creates a ClientThread which creates a socket and runs until the socket is connected, whenever a message is received i use a handler to post message to another thread called ProcessDataThread, now on receiving some messages from server i need to update UI related stuff from ProcessDataThread, I searched around alot and i found these two ways runonUiThread function which i guess can only be run from the Activity Class which is useless and the Asynctask method which i am not sure how to pass the activity context to...

Here is the code

The code executed when clicked on Login Button in the MainActivity

public void onLoginClick(View view)
{
    global_constants.clientObject = new ClientThread();
    global_constants.clientThread = new Thread(global_constants.clientObject);
    global_constants.clientThread.start();
}

The code in ClientThread run method

public class ClientThread implements Runnable {
.......
    @Override
    public void run() {
    ......
        while(!Thread.currentThread().isInterrupted() && (!CloseThread))
        {
            byte[] buff;
            ....
            global_constants.updateConversationHandler.post(new ProcessDataThread(buff));
        }
    }
}

The method code in ProcessDataThread after parsing out the incoming data and stuff

public class ProcessDataThread implements Runnable {
.........
    void ProcessLoginFailedPacket(byte[] buff)
    {
        // how to call the UI thread from here for updating some UI elements??
    }
}

[EDIT]

i stored the activity context in a global variable and then did it this way, but i dont know whether it will be safer or not

((Activity)global_constants.MainContext).runOnUiThread(new Runnable(){
    public void run()
    {
        TextView txtErr = (TextView) ((Activity)global_constants.MainContext).findViewById(R.id.errMsg);
        txtErr.setVisibility(0);
        txtErr.setText(reason);
    } 
});

解决方案

You can post a runnable which does the UI operation to main thread as follows,

public class Utils {

    public static void runOnUiThread(Runnable runnable){
        final Handler UIHandler = new Handler(Looper.getMainLooper());
        UIHandler .post(runnable);
    } 
}

Utils.runOnUiThread(new Runnable() {
     @Override
     public void run() {
         // UI updation related code.
     }
});

这篇关于机器人:从另一个类另一个线程更新界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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