I’m working on Android UI Thread, and there is one time-consuming work, so I put It on another thread. There two thing I want:
1) after this sub-thread run, UI Thread will start to do workA
2)this workA will take some data that create in sub-thread.
Here my solution:
Car car;
public void onResume(){
super.onResume();
Thread t = new Thread(){
car = new Car();
car.takePetrol; //take car full petrol
}
});
t.join();
count_How_far_Car_can_go(car.getPetrol);
}
The problem in my code is: I have use t.join(); to wait thread t finish. And this will “Block” the UI Thread. Of course, that is not what I want. (because I make another thread so UI Thread still can work smoothly)
Who has another solution for my problem, please help me 🙂
thanks 🙂
Check out
AsyncTaskclass fromandroid.os.AsyncTaskpackage.EDIT: Sorry for the quick answer. I have improved it according to OP’s needs.
And trigger it like this
If you need to pass parameters to your
AsyncTask, you can add your parameter object to class definition, and of course pass your parameter object while executing your task.With the parameter added to definition, you should instantiate with