如何将值从类传递到Activity - Android [英] How to pass values from a Class to Activity - Android

查看:80
本文介绍了如何将值从类传递到Activity - Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于课程/任务/活动的新手问题。我来自C所以我不知道这是否是一个很好的方法来做我需要的。

I have a newbie question about Class/Task/Activity. I'm coming from C so I don't know if it's a good approach to do what I need.

我创建了一个类:

public class UDPServer {

    private MyDatagramReceiver myDatagramReceiver = null;

    private static int MAX_UDP_DATAGRAM_LEN = 1024;
    private static int UDP_SERVER_PORT = 5000;

    public void start() {
        myDatagramReceiver = new MyDatagramReceiver();
        myDatagramReceiver.start();
    }

    public void kill() {
        myDatagramReceiver.kill();
    }

    private class MyDatagramReceiver extends Thread {
        private boolean bKeepRunning = true;
        private String lastMessage = "";

        public void run() {
            String message;
            byte[] lmessage = new byte[MAX_UDP_DATAGRAM_LEN];
            DatagramPacket packet = new DatagramPacket(lmessage, lmessage.length);
            DatagramSocket socket = null;

            try
            {
                socket = new DatagramSocket(UDP_SERVER_PORT);

                while(bKeepRunning)
                {
                    socket.receive(packet);
                    message = new String(lmessage, 0, packet.getLength());
                    lastMessage = message;

                    //Here should call activity method

                });
                }

            }
            catch (Throwable e)
            {
                e.printStackTrace();
            }
            finally
            {
                if (socket != null)
                {
                    socket.close();
                }
            }
        }

        public void kill() {
            bKeepRunning = false;
        }
    }
}

我的活动内部我已经:

@Override
public void onResume() {
    super.onResume();

    mUDPServer = new UDPServer();
    mUDPServer.start();
}

@Override
public void onPause() {
    super.onPause();

    mUDPServer.kill();
}

现在,每次我收到一个数据包,我希望这个线程/类通过收到一个Activity方法的数据包,详细说明(做一些计算或更新一些UI ecc ..)这个传入的数据。但我无法想象如何做到这一点,也许我的做法不正确。我可以将线程代码放在Activity中,但它似乎使代码的可读性降低。

Now, every time I received a packet I want that this thread/class pass received packet to an Activity method that elaborate(do some calculation or update some UI ecc..) this incoming data. But I can't figure how to do this, maybe my approach is not correct. I can place thread code inside Activity but it seems to make code less readable.

建议怎么做?

@Anshul Jain CODE UPDATE:

@Anshul Jain CODE UPDATE:

    public class Main_activity extends Activity implements Interface_UDPServer{
    TextView recived_message;

    UDPServer mUDPServer;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        recived_message = (TextView)findViewById(R.id.recived_message);
    }

    @Override
    public void onResume() {
        super.onResume();

        mUDPServer = new UDPServer(this);
        mUDPServer.start();
    }

    @Override
    public void onPause() {
        super.onPause();

        mUDPServer.kill();
    }

    public void sendData(final String str){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                recived_message.setText(str);
            }
        });
    }

}

XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="@+id/linearLayout"
        android:layout_alignLeft="@+id/linearLayout"
        android:layout_alignStart="@+id/linearLayout">
    <TextView
        android:id="@+id/recived_message"
        android:layout_width="200dp"
        android:layout_height="35dp"
        android:textColor="#444444"
        android:textSize="20dp" />
    </LinearLayout>
</RelativeLayout>


推荐答案

您可以使用Callback来达到此目的。

You can use Callback for this purpose.

定义一些界面,如

public interface MyCustomInterface(){
    public void sendData(String str);
}

现在让你的Activity实现这个界面。

Now let your Activity implement this interface.

public class MyActivity implements MyCustomInterface {

@Override
public void sendData(String str){

Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
  @Override
  public void run() {
    recived_message.setText(str);
  }
});
}
}

现在在UDPServer.java中,编写以下代码

Now in UDPServer.java, write the following code

public class UDPServer {

private MyCustomInterface interface;

UDPServer(MyCustomInterface interface){
 this.interface = interface; 
}

}

现在每当有一些数据可用时让我们说一个字符串,你可以像这样发送

Now whenever you have some data available lets say a string, you can send it like this

interface.sendData(str);

这篇关于如何将值从类传递到Activity - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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