Cannot execute task: the task has already been executed (a task can be executed only once)
http://stackoverflow.com/questions/19345118/async-task-cant-be-executed-twice
解答:
As the exception itself explains, you cannot execute an
AsyncTask
more than once, unless you create a new
instance of it and call .execute
.
For example:
async = new AsyncTask();
async.execute();
*in order to execute more than once, you need to re-create the instance (using
new
) the number of times you want to execute it.
但有人說沒作用
問題:
Android restart AsyncTask
http://stackoverflow.com/questions/11586704/android-restart-asynctask
解答:
改成使用Thread 和 HandleMessage實作
Thank you for all your comments they helped a lot. I decided to do away with the AsyncTask. I ended using a normal runnable Thread and using Handlers to post messages back to the UI thread. here is the code:
// Start thread here only for IsSocketConnected
new Thread(new Runnable() {
public void run() {
//Add your code here..
IsSocketConnected();
}
}).start();
// handler that deals with updating UI
public Handler myUIHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
if (msg.what == Bluetooth.STATE_CONNECTED)
{
//Update UI here...
Log.d(TAG, "Connected");
// Discover available devices settings and create buttons
CreateButtons(btnList);
} else if(msg.what == Bluetooth.STATE_NONE) {
Log.d(TAG, "NOT Connected");
}
}
// in the IsSocketConnected() I call this
Message theMessage = myUIHandler.obtainMessage(Bluetooth.STATE_CONNECTED);
myUIHandler.sendMessage(theMessage);//Sends the message to the UI handler.
This is working so far. Thank you again. Hope this helps someone
沒有留言:
張貼留言