我有一个Android应用程序,在Asynctask中包含一个Socket,它可以工作,但我不知道这是否是实现它的正确方法。
另一件事,我的onProgressUpdate更新我的列表适配器和导致延迟,当我播放动画。
有人有好的解决方案吗?您确定您没有在UI线程上进行任何计算吗?
查看这个页面,这里有一些关于如何在Android中使用各种线程机制的指导。
如果这不能解决您的问题,请尝试发布处理列表填充的部分代码。
我是这样做的…(仅用于Socket连接)
private class EstablishConnectionTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
boolean ok = false;
dataSend = false;
//establish the Connection to the server and returns if it succeed or not
ok = client.createClient(server, port);
//try to send the data and return if it succeed or not
if(ok) dataSend = client.sendData(profilename);
if(dataSend) {
//close the connection
client.closeConnection();
}
return null;
}
protected void onPreExecute() {
//bring a process Dialog to the front
showDialog(DIALOG_CONNECTING);
}
protected void onPostExecute(Void result) {
removeDialog(DIALOG_CONNECTING);
if(dataSend) {
Toast.makeText(Activity_sendXML.this, "xml versendet", 2000).show();
}
else{
showDialog(DIALOG_CONNECTION_REFUSED);
}
}
}