ऐसा लगता है कि आप मुख्य थ्रेड से कहीं और से AsyncTask शुरू कर सकते हैं लेकिन वास्तव में postExecute मुख्य थ्रेड पर निष्पादित किया जाता है। यहाँ मेरी परीक्षा है, मुझे बताओ अगर यह आप के लिए तार्किक लगता है:
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
क्यों पद पर अमल मैं नहीं जानता कि 4 बार किया जाता है:
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());
}
}
यहाँ कार्यों के बाद स्टैकट्रेस है हालांकि ...
प्रलेखन * प्रकट होता है * मुझे भी भ्रामक करता है, जबकि मैं पूरे भाग में नहीं चलाता अपवाद जो आपको मिल रहा है यह मेरा अनुभव रहा है कि यूआई थ्रेड 'ऑनपोस्टएक्सक्यूट' के बाहर 'AsyncTask' का उपयोग करते समय कॉलिंग थ्रेड से होता है और जरूरी नहीं कि यूआई थ्रेड (तब यूआईएलर की आवश्यकता होती है यदि यूआई को छेड़छाड़ की जरूरत है)। –