[英]Android AsyncTask in another AsyncTask
I want to run an AsyncTask in another AsyncTask with this code:我想使用以下代码在另一个 AsyncTask 中运行 AsyncTask:
protected Integer doInBackground(Void... params) {
InetAddress inetAddress;
try {
while (true) {
inetAddress = InetAddress.getByName("192.168.1.1");
mSocket = new Socket(inetAddress, 4444);
SocketData data = new SocketData();
GetPacket pack = new GetPacket();
AsyncTask<SocketData, Integer, Integer> running = pack.execute(data);
but have an error : Method must be called from the main thread android current inferred thread is worker但是有一个错误:必须从主线程调用方法android当前推断的线程是worker
Do it in the onProgressUpdate
this method is running on the MainThread在onProgressUpdate
此方法在 MainThread 上运行
protected Integer doInBackground(Void... params) {
publishProgress(data);
}
protected void onProgressUpdate(SocketData... data) {
GetPacket pack = new GetPacket();
AsyncTask<SocketData, Integer, Integer> running = pack.execute(data[0]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.