1

我正在尝试使用 ASyncTask 在我的 android 应用程序的后台处理网络。SendData 中发生的任何事情都在一个单独的线程中。后台线程开始运行,打印前两条日志消息。然后它挂起。我对android和sockets都很陌生,所以如果这有很多错误我不会感到惊讶。

package light24bulbs.ioio.testing;

import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.BaseIOIOLooper;
import ioio.lib.util.IOIOLooper;
import ioio.lib.util.android.IOIOActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.Button;
import android.widget.Toast;
import ioio.lib.api.PwmOutput;
import android.view.*;
import android.widget.TextView;
import android.util.Log;
import light24bulbs.ioio.testing.R;
import java.io.*;
import java.net.*;
import android.os.AsyncTask;
import android.view.View.*;

public class MainActivity extends IOIOActivity {
    private SeekBar bar_;
    private final int servoPin = 11;
    private final int freq = 100;
    private final String serverIP="192.168.1.41";
    TextView progressText;
    Button open;
    int progress;
    SendData senddata;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bar_ = (SeekBar) findViewById(R.id.seekBar1);
        TextView progressText = (TextView) findViewById(R.id.textView1);
        open = (Button) findViewById(R.id.start);
        open.setOnClickListener(openSocket);
        senddata = new SendData();
        senddata.execute(serverIP,null,null);


    }


    class Looper extends BaseIOIOLooper {
        /** The on-board LED. */
        private PwmOutput servo_;

        //happens when IOIO module is connected
        @Override
        protected void setup() throws ConnectionLostException {

            servo_ = ioio_.openPwmOutput(servoPin, freq);



        }

//loops forever once everything else has been set up
        @Override
        public void loop() throws ConnectionLostException {

            servo_.setPulseWidth(600+(bar_.getProgress()*20));
            //servo_.setPulseWidth(800);
             progress = bar_.getProgress();
             senddata.onProgressUpdate(progress);

             try{
                 Thread.sleep(20);
             }catch(InterruptedException e){

             }
        }
    }

    @Override
    protected IOIOLooper createIOIOLooper() {
        return new Looper();
    }


private class SendData extends AsyncTask<String, Integer, Boolean>{
    Socket clientSocket;
    DataOutputStream toServer;
    PrintStream pStream;
    @Override
    protected Boolean doInBackground(String... IP){
        Log.i("AsyncTask","doInBackground started running");
        Log.i("AsyncTask","attempting to connect to ip: "+IP[0]);
        try{


        clientSocket = new Socket(IP[0],4488);
        Log.i("AsyncTask","Created socket"+String.valueOf(clientSocket));
        toServer = new DataOutputStream(clientSocket.getOutputStream());
        }catch(IOException e){
            Log.i("AsyncTask","IO Exception while connecting socket- doInbackground()");
            return false;
        }
        Log.i("AsyncTask","Port connected! "+IP[0]);
        return true;
    }
    @Override
    protected void onProgressUpdate(Integer...pos){
        try{
        toServer.writeBytes(String.valueOf(pos[0]));
        }catch(IOException e){
            Log.i("AsyncTask","Failed to send the position");
        }
    }





}
}
4

1 回答 1

1

对于初学者,protected void onProgressUpdate(Integer...pos)在 UI 线程上运行。

这通常不是向网络发送内容的最佳位置 - 想法是您使用此方法更新 UI,例如TextView包含计数器或进度对话框的 update 。

其次,您的实现#doInBackground(..)将设置套接字并立即退出,返回Thread到线程池支持AsyncTask

如果你想向服务器发送多个包,你将不得不在这里创建一个循环。此外 - 你的调用toServer(并进行任何循环)也应该在doInBackground()- 如果你想更新你的 UI - 调用#publishProgress(..) 来自 #doInBackground()- 它将传递你想要可视化的数据到#onProgressUpdate(..).

于 2012-05-14T09:12:39.470 回答