我正在尝试使用 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");
}
}
}
}