如何通知活动时GlobalVariables改变 [英] How to notify an activity when GlobalVariables are changed

查看:102
本文介绍了如何通知活动时GlobalVariables改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个是通过USB线连接到电脑上的Andr​​oid应用程序。我用一个TCPSERVER类发送消息,并听取。例如:

I have an android application that is connected to the computer via USB cable. I use a TCPServer Class to send messages and listen. For example:

当我发一条消息,如:请求:X
我得到的回应:响应:X:55​​

When I send a message like: request:x
I get the response: response:x:55

我要根据我得到的回应对我的行为做出改变。目前,我暂时通过传递活动和活动类对象的TCPSERVER的构造

I need to make changes on my activity according to the response I get. At the moment I temporarily solved the problem by passing activity and activity class object to the TCPServer's constructor

public TCPServer(int portNum, Activity activity, IntroActivity ia) {
    super();
    port = portNum;
    this.activity = activity;
    this.ia = ia;
}    

然后后,我收到了答复:

Then after I receive the response:

void updateButton(final int color, final String txt) {
    activity.runOnUiThread(new Runnable() {
         public void run() {
             ia.getConnectionButton().setBackgroundColor(color);
             ia.getConnectionButton().setText(txt);
        }
    });
}   

正如你看到的,这是没有效果的。我需要以某种方式通知每当接收到相关变量的活动。我使用的是类的GlobalVariables和改变那些静态变量听()之后,但我有麻烦通知活动。

As you see, this is not effective at all. I need to somehow notify the activity whenever a relevant variable is received. I use a Class for GlobalVariables and change those static variables after listen(), however I am having troubles notifying the activity.

推荐答案

首先,它几乎总是不好的做法是传递活动的实例。这是一个时间,当它是坏的。

First of all, it is almost always bad practice to pass Activity instances around. This is a time when it's bad.

定义的接口,并使用回调让活动知道的响应已被接收。

Define an interface and use a callback to let the activity know that a response has been received.

public interface ResponseReceivedListener {
    void onResponseReceived(int arg1, string arg2); ..<----add arguments you want to pass back
}

在你TCPSERVER类

In your TCPServer class

ArrayList<ResponseReceivedListener> listeners = new ArrayList<ResponseReceivedListener>();

...

public void addResponseReceivedListener(ResponseReceivedListener listener){
    if (!listeners.contains(listener){
        listeners.add(listener);
    }
}

public void removeResponseReceivedListener(ResponseReceivedListener listener){
    if (listeners.contains(listener){
        listeners.remove(listener);
    }
}

当您收到响应

for (ResponseReceivedListener listener:listeners){
   listener.onResponseReceived(arg1, arg2);
}

在你的活动:

public class MainActivity extends Activity implements ResponseReceivedListener {

...

@Override
public void onCreate(Bundle savedInstanceState) 
{
    ...
    tcpServer.setResponseReceivedistener(this);
    ...
}

public void onResponseReceived(int arg1, string arg2){
   // do whatever you need to do
}

所有的记忆,所以请原谅错别字。

All from memory so please excuse typos.

这方法去耦的类。 TCP服务器没有知识的活动。它只是回调注册的侦听器。这些听众可能是活动,他们可能是服务。他们可能是MySparklyUnicorn的实例。服务器不知道也不关心。它只是说:如果任何人的兴趣,我已经收到了响应,下面是详细信息。

This approach decouples the classes. The TCP Server has no knowledge of the activities. It simply calls back to any listeners registered. Those listeners might be Activities, they might be services. They might be instances of MySparklyUnicorn. The server neither knows nor cares. It simply says "if anyone's interested, I've received a response and here are the details".

这篇关于如何通知活动时GlobalVariables改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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