当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Best way to launch different tasks

问题描述:

I am developing an application for blind people. I have to work all the time with the TextToSpeech module, GPS and Network connection.

I need to do a query like this: Consult the GPS, do a JSON call and calling the TextToSpeech(TTS) module.

I am wondering the best way to deal with the different tasks which communicate with the UI main thread. I have seen so far:

Handler objects and AsyncTask class.

I have to launch each task sequentially, so I want to call the TTS after retrieving data from the network. So I have used "mHandler.post(Runnable)" and inside that runnable calling another one and so on.

However I have seen that is recommendable the use of the AsynTask class. But in this case I think I have to implement a different class for every task, whereas I don't know if those tasks will execute sequentially. Something like:

AsyntaskClass1 at1;

AsyntaskClass2 at2;

AsyntaskClass3 at3;

at1.execute();

at2.execute();

at3.execute();

Will that tasks execute in order? Cause the TTS module have to wait for the network task to finish...

Thanks for your help,

BR.David.

网友答案:

No they will be executed almost simultaneously... i.e. the second asynctask will not wait for first one to finish.

I suggest you to club all the tasks in one single AsyncTask and use publishprogress() to update the UI after each task finishes.

so you can start that AsyncTask after the network finishes and the UI will get updated sequentially

eg.

public Object doInBackground(Object... params){
   //do task one's stuff
   publishProgress(1);
 // do task two's stuff
publishProgress(2)
   //do task 3
publishProgress(3)


}
public void onProgressUpdate(Integer... int){
switch(int[0]){
case 1:
//ui update for task 1
case 2:
//ui update for task 3
case 3:
//ui update for task 3
}
网友答案:

You could everything with a single AsyncTask class:

private class AsyncTask1 extends AsyncTask<Object, Void, Object> {

 @Override
 protected String doInBackground(Object... args) {
    // Get a result from the GPS.

    // Make a network call

    // Communicate with the TTS 
  }

 @Override
 protected void onPostExecute(Object result) {
     // Return a result to the main thread
     // You are only allowed to touch the UI here
  }

}
网友答案:

The execution order is not guaranteed in your example. The UI thread would launch the 3 AsyncTasks. These three tasks would then execute concurrently.

If you want to execute the code in the tasks synchronously you have at least a couple of ways to acomplish it:

Combine the three tasks into one where you have ordered the work to be done sequentially i.e:

AsyncTaskCombined task = new AsyncTaskCombined();
task.execute();

private class AsyncTaskCombined extends AsyncTask<Void, Void, Void> {
    protected Bitmap doInBackground(Void... urls) {
        // Initialize classes that implement the work logic
        Work1 work1;  
        Work2 work2;
        Work3 work3;
        // Do the work sequentially...
        work1.doWork();
        work2.doWork();
        work3.doWork();
    }
}

Or potentially you could have 3 different AsyncTask and then in the onPostExecute of the first task create and call the second AsyncTask and in the second AsyncTask's onPostExecute create and call the third and last AsyncTask. However that seems a bit contrieved to me...

// Emil H

分享给朋友:
您可能感兴趣的文章:
随机阅读: