gpt4 book ai didi

android - Android 应用程序监听传入 TCP/IP 消息的最佳方式是什么?

转载 作者:太空狗 更新时间:2023-10-29 13:36:37 29 4
gpt4 key购买 nike

我想编写一个应用程序,只要它在服务器范围内(在同一个 WiFi 网络上)就会监听警报消息。做这个的最好方式是什么?

我是否应该简单地编写一个充当 TCP/IP 服务器的服务,当它接收到消息时,让它们生成一个 Android 通知?或者有更好的方法来做到这一点?

我需要这个应用程序在 Android 运行时一直运行。
我如何确保我的“监听器”服务始终在运行?

提前致谢..

莱夫斯特

最佳答案

我有同样的问题......我想使用 cometd ,但我没有太多时间研究它......

你可以在这里看到一些样本..
http://blogs.webtide.com/dyu/entry/android_chat_using_jetty_cometd

我选择了无限循环进行通信

这是我的代码..

public class ServerActivity extends Activity {
private TextView serverStatus;

// default ip
public static String SERVERIP = "10.0.2.15";

// designate a port
public static final int SERVERPORT = 12345;

private Handler handler = new Handler();

private ServerSocket serverSocket;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.server);
serverStatus = (TextView) findViewById(R.id.server_status);

SERVERIP = getLocalIpAddress();

Thread fst = new Thread(new ServerThread());
fst.start();
}

public class ServerThread implements Runnable {
String line = "";

public void run() {

try {
if (SERVERIP != null) {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Listening on IP: " + SERVERIP);
}
});
serverSocket = new ServerSocket(SERVERPORT);
while (true) {
// listen for incoming clients
Socket client = serverSocket.accept();
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Connected.");
}
});

try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
// byte[] resultBuff = new byte[0];
// byte[] buff = new byte[1024];
// int k = -1;
// while((k = client.getInputStream().read(buff, 0, buff.length)) > -1) {
// byte[] tbuff = new byte[resultBuff.length + k]; // temp buffer size = bytes already read + bytes last read
// System.arraycopy(resultBuff, 0, tbuff, 0, resultBuff.length); // copy previous bytes
//System.arraycopy(buff, 0, tbuff, resultBuff.length, k); // copy current lot
//resultBuff = tbuff; // call the temp buffer as your result buff
//}
//System.out.println(resultBuff.length + " bytes read.");




while ((line = in.readLine()) != null) {
Log.d("ServerActivity", line);


handler.post(new Runnable() {
@Override
public void run() {

serverStatus.setText(line);
}
});
}
break;
} catch (Exception e) {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones.");
}
});
e.printStackTrace();
}
}
} else {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Couldn't detect internet connection.");
}
});
}
} catch (Exception e) {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Error");
}
});
e.printStackTrace();
}
}
}

关于android - Android 应用程序监听传入 TCP/IP 消息的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9856523/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com