我正在使用AsyncTask来执行后台任务。任务是在一个while循环中。 如果后台任务仍在运行,如何更新我的UI,因为后执行剂量不起作用。
由于
protected String doInBackground(Void ... params){
while(true){
Log.i(LOG_TAG, "Executing Background Task");
try{
String dataFromBT = btc.getData();
Log.i(LOG_TAG, "BT Data: "+dataFromBT);
if(dataFromBT.contains("B1")){
dataFromBT = "Warning Message 1";
}
if(dataFromBT.contains("B2"))
{
dataFromBT="Warning Message 2";
}
if(dataFromBT.contains("B3")){
dataFromBT="Warning Message 3";
}
if(dataFromBT.contains("B4")){
dataFromBT="Warning Message 4";
}
if (groupOwnerAddress!=null) {
Log.i(LOG_TAG, "Info is not null mobiles connected");
// This is the server side
if (isthisthegrpowner == true) {
Log.w(LOG_TAG, "Group Owner: I am the Group Owner ");
Log.w(LOG_TAG, "Group Owner: Opening a Server Socket");
ServerSocket serverSocket;
serverSocket = new ServerSocket(8988);
Log.w(LOG_TAG, "Group Owner: Server Socket Opened, waiting for PEER");
Socket client = serverSocket.accept();
Log.w(LOG_TAG, "Group Owner: Server Connection Done");
serverSocket.setReuseAddress(true);
try{
// Get client IP from Socket
clientipadd = client.getRemoteSocketAddress();
clientport = client.getPort();
String clientip = clientipadd.toString();
Log.w(LOG_TAG, "Group Owner: Client IP from socket found: " + clientip);
Log.w(LOG_TAG, "Group Owner: Input Stream Started");
InputStream inputstream = client.getInputStream();
ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
String response = "";
int bytesRead;
while ((bytesRead = inputstream.read(buffer)) != -1){
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
}
// Split the string sent from the client and add it to the HashMap
String input = response;
final String[] splitStringArray = input.split(" ");
String a = splitStringArray[0];
String b = splitStringArray[1];
String c = splitStringArray[2];
String d = splitStringArray[3];
String e = splitStringArray[4];
Log.w(LOG_TAG, "Group Owner: Response from client split: " + " 1: " + a + " 2: "+ b + " 3:" + c + " 4: " + d + " 5: " + e );
data.put(clientip, new VehicleInfoEntry(a, b , c));
client.shutdownInput();
Log.w(LOG_TAG, "Group Owner: Reply from Peer: " + response);
}finally{
Log.w(LOG_TAG, "Group Owner: Output Stream started");
OutputStream stream = client.getOutputStream();
PrintStream printStream = new PrintStream(stream);
printStream.print("hello hellomac hellodata" + Latitude + " " + Longitude + dataFromBT);
Log.w(LOG_TAG, "Group Owner: Output Stream finished");
serverSocket.close();
Log.w(LOG_TAG, "Group Owner: Socket Closed");
}
// This is the client side
} else{
Log.w(LOG_TAG, "PEER: I am a PEER");
InetAddress ownerAdd = groupOwnerAddress;
int ownerPort = 8988;
Socket server = new Socket();
try {
server.connect((new InetSocketAddress(ownerAdd, ownerPort)));
Log.w(LOG_TAG, "PEER: Socket done ");
Log.w(LOG_TAG, "PEER: Output Stream Started ");
OutputStream stream = server.getOutputStream();
PrintStream printStream = new PrintStream(stream);
printStream.print("hello hellomac hellodata" + " " + Latitude + " " + Longitude + dataFromBT);
Log.w(LOG_TAG, "PEER: Output Stream Done");
server.shutdownOutput();
// printStream.close();
} finally {
Log.w(LOG_TAG, "PEER: Input Stream Started");
InputStream inputstream = server.getInputStream();
ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
String response = "";
int bytesRead;
while ((bytesRead = inputstream.read(buffer)) != -1){
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
Log.w(LOG_TAG, "PEER: Reply from Group Owner: " + response);
server.close();
Log.w(LOG_TAG, "PEER: Server socket closed");
}
}
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally {
}try {
Thread.sleep(2000); // changed to 5000 for other peer
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
如果您想使用后台线程中的用户界面,请在runOnUiThread
doInBackground
,如下所示
runOnUiThread(new Runnable(){
public void run() {
// update UI
}
答案 1 :(得分:1)
使用publishProgress
方法。您在onProgressUpdate
中执行的任何操作都在UI线程上运行
private class TestAsyncTask extends AsyncTask<Void, String, Void> {
@Override
protected Void doInBackground(Void... voids) {
SystemClock.sleep(1000);
publishProgress("Test");
SystemClock.sleep(1000);
return null;
}
@Override
protected void onProgressUpdate(String... values) {
String test = values[0];
// Do something with it
}
}
protected void onProgressUpdate(Progress ... values)
在API级别3中添加在UI线程之后运行 调用publishProgress(Progress ...)。指定的值是 值传递给publishProgress(Progress ...)。
参数值表示进度的值。