我目前正在开发android和服务器之间的套接字通信,这是一个在终端中运行的简单java程序。一切进展顺利,但当我关闭应用程序时,logCat 中总是出现警告:
IInputConnectionWrapper showStatusIcon on inactive InputConnection
我正在互联网上搜索以找出我在 StackOverflow Similar Problem中找到的帖子的问题。不同之处在于我可以在我的程序中很好地发送和接收信息。这个类似问题的答案是连接没有关闭。这是否意味着我socket.close();
在手术后没有打电话?这导致了更复杂的实施问题。
首先,我想要一个单独的静态套接字来监听并发送到服务器。因为我可能不会在每次传输东西时关闭套接字,所以我只是在侦听器完成工作后关闭它。
详细代码贴在下面
我在构造函数中将连接初始化为:
client = new Socket(mServerName, mport);
out = new DataOutputStream(client.getOutputStream());
inFromServer = new InputStreamReader(client.getInputStream());
reader = new BufferedReader(inFromServer);
并让他们在整个过程中都在那里。
我将从 android 到服务器的传输写入如下函数:
public void sendRequest(int type, int what1, int what2, Object data)
{
try{
if(client.isConnected())
{
out.writeUTF(encoded(type,what1,what2,data) + "\n");
}
}catch(IOException e){
Log.e(TAG, "IOException at SendRequest");
e.printStackTrace();
}
}
新线程中的侦听器:
try {
String line = null;
while ((line = reader.readLine()) != null)
{
ReceiveHandler(line);
}
} catch (IOException e) {
Log.e(TAG, "IOException at StartListen");
e.printStackTrace();
}finally{
try {
// The Only Place that I close the Socket
inFromServer.close();
out.close();
reader.close();
client.close();
} catch (IOException e) {
Log.e(TAG, "Close Socket with IOException " + e.toString());
e.printStackTrace();
}
}
我的问题是:
我的实现有问题还是有更好的方法来做到这一点?
非常感谢你的帮助!