Sembra che si può iniziare AsyncTask da qualche altra parte che il thread principale, ma il postExecute è realmente eseguito sul thread principale. Qui è la mia prova, ditemi se vi sembra logico per voi:
public class AsyncLoader extends AsyncTask<Context, String, Boolean> {
private ConnectivityManager cm;
private ArrayList<Contact> nameList;
private Context ctx;
private static int i = 0;
@Override
protected Boolean doInBackground(Context... params) {
ctx = params[0];
cm = (ConnectivityManager) params[0].getSystemService(Activity.CONNECTIVITY_SERVICE);
if (i < 3) {
i++;
new AsyncLoader().execute(ctx);
System.out.println("Thread name (from doInBackground) : " + Thread.currentThread().getName());
System.out.println("compteur async " + i);
}
if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable()) {
nameList = (ArrayList<Contact>) ContactLoaderXml.loadContactsData(); //this is a loading from the web + sax parser
return true;
} else {
return false;
}
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
System.out.println("Thread name (from postexecute) : " + Thread.currentThread().getName());
}
}
Ecco la stacktrace dopo i compiti:
I/System.out( 641): Thread name (from doInBackground) : AsyncTask #1
I/System.out( 641): compteur async 1
I/System.out( 641): Thread name (from doInBackground) : AsyncTask #2
I/System.out( 641): compteur async 2
I/System.out( 641): Thread name (from doInBackground) : AsyncTask #3
I/System.out( 641): compteur async 3
I/System.out( 641): Thread name (from postexecute) : main
I/System.out( 641): Thread name (from postexecute) : main
I/System.out( 641): Thread name (from postexecute) : main
I/System.out( 641): Thread name (from postexecute) : main
Non so il motivo per cui il palo eseguire è fatto 4 volte comunque ...
La documentazione * appare * anche fuorviante, mentre non ho attraversato il thread l'eccezione che stai ottenendo è stata la mia esperienza che quando si utilizza un 'AsyncTask' al di fuori dell'interfaccia utente' onPostExecute' si verifica dal thread chiamante e non necessariamente dal thread UI (quindi richiede un 'Handler' se l'interfaccia utente deve essere manipolata). –