大家好我有问题,我正在进行多个asyc任务,我想在那些任务完成时调用Method。请检查。
for (ClientsDetailDAO clientsDetailDAO : clients) {
Log.d(TAG, "onConnected for");
Addresses addresses = clientsDetailDAO.getAddress();
StringBuilder sb = null;
if (addresses != null) {
Log.d(TAG, "onConnected address not null");
sb = new StringBuilder(addresses.getStreetAddress())
.append(", ").append(addresses.getCity())
.append(", ").append(addresses.getState())
.append(", ").append(addresses.getZip());
Log.d(TAG, "Address formatted: " + sb.toString());
mAddressAsyncTask = new GetAddressAsyncTask(
clientsDetailDAO);
mAddressAsyncTask.execute(sb.toString());
}
}
私有类GetAddressAsyncTask扩展AsyncTask { private ClientsDetailDAO clientsDetailDAO;
public GetAddressAsyncTask(ClientsDetailDAO clientsDetailDAO) {
this.clientsDetailDAO = clientsDetailDAO;
}
@Override
protected LatLng doInBackground(String... params) {
Geocoder geoCoder = new Geocoder(getActivity());
LatLng latLng = null;
try {
List<Address> addresses = geoCoder.getFromLocationName(parmas.toString,
1);
Log.i(TAG, "param " + params[0] + " addresses size: "
+ addresses.size());
if (addresses.size() > 0) {
latLng = new LatLng(addresses.get(0).getLatitude(),
addresses.get(0).getLongitude());
}
} catch (Exception e) {
e.printStackTrace();
}
if (isCancelled())
return null;
return latLng;
}
@Override
protected void onPostExecute(LatLng result) {
if (result != null) {
Log.d(TAG, "onConnected latLng not null");
clientsDetailDAO.setLatitude(result.latitude);
clientsDetailDAO.setLongitude(result.longitude);
Toast.makeText(getActivity(), "in post" + result.latitude,
Toast.LENGTH_LONG).show();
} else {
Log.d(TAG, "onConnected latLng null");
}
}
}
在finsih这些任务后我想调用setUpMarker();方法。
答案 0 :(得分:0)
调用setUpMarker()
int最新的asynctask&#39; onPost
方法
当您有3个异步时的示例:
onPost
onPost
我们需要在onPost
中调用它,以确保在调用其他asyncTask
之前完成第一个asyncTask
,以避免竞争条件。
答案 1 :(得分:0)
您可以将所有任务存储在一个数组中,并在onPostExecute中检查这些任务是否已完成(使用getStatus方法)。
答案 2 :(得分:0)
如果有固定数量的asyncs,请说2,采用全局int [2]数组。
onPostExecute{ // Async 1
array[0]=1;
if(checkIfAllArraySet())
finalMethod();
}
onPostExecute{ // Async 2
array[1]=1;
if(checkIfAllArraySet())
finalMethod();
}
boolean checkIfAllArraySet()
{
// check if all arra[] items are 1 with a loop
}
答案 3 :(得分:0)
启动AsyncTask(Parent),启动多个AsyncTasks(Child)。每个孩子在完成时通知父母。
public class ParentAsyncTask extends AsyncTask... implements OnTaskCompletedListener{
private int tasksRunning = 0;
@Override
protected LatLng doInBackground(...) {
tasksRunning = clients.size();
// spawn multiple AsyncTask
for (ClientsDetailDAO clientsDetailDAO : clients) {
Log.d(TAG, "onConnected for");
Addresses addresses = clientsDetailDAO.getAddress();
StringBuilder sb = null;
if (addresses != null) {
Log.d(TAG, "onConnected address not null");
sb = new StringBuilder(addresses.getStreetAddress())
.append(", ").append(addresses.getCity())
.append(", ").append(addresses.getState())
.append(", ").append(addresses.getZip());
Log.d(TAG, "Address formatted: " + sb.toString());
mAddressAsyncTask = new GetAddressAsyncTask(
clientsDetailDAO);
mAddressAsyncTask.execute(sb.toString());
}
}
}
@Override
void onTaskCompleted(...){
tasksRunning--;
if (tasksRunning == 0){
doSomethingAfterAllTasksFinished();
}
}
}