PDA

View Full Version : چرخه حیات AsyncTask



Sync.Master
چهارشنبه 24 مهر 1392, 09:28 صبح
سلام

عیدتون مبارک

111912


از AsyncTask این صفحه استفاده کردم برای دانلود ، وقتی ازش استفاده میکنم دوباره که میخوام دانلود کنم ProgressDialog باز میشه ولی شروع به انجام پروسه دانلود نمیکنه و همینطور باقی میمونه ، اینترنتم هم اصلا مشکلی نداره و در همون حین چک میکنم که ارتباط برقرار هست یا نه.

http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog

آیا AsyncTask ها باید بعد از استفاده stop (خلاصه از بین برن یا نه) بشن؟ اگه آره چجوری؟

یه سوال دیگه ، الان بجز دسترسی که تو این صفحه گفته هیچ دسترسی نداره برنامه و هم میتونه دانلود کنه و هم تو sd card فایل رو ذخیره کنه! یعنی یه برنامه میتونه تو پس زمینه اطلاعات ما رو بدون نیاز به دسترسی بفرسته یا یه چیز مخرب رو دریافت کنه؟

Sync.Master
یک شنبه 28 مهر 1392, 12:02 عصر
جواب سوال دومم : مشکل از Genymotion بود که بدون دسترسی هم برنامه کار میکرد ولی رو شبیه ساز اصلی خطای نبود دسترسی میداد.

مهران رسا
یک شنبه 28 مهر 1392, 19:09 عصر
اینجا رو هم ببینید بد نیست : http://developer.android.com/reference/android/os/AsyncTask.html




6. AsyncTask
6.1. Purpose of the AsyncTask class
The AsyncTask class encapsulates the creation of a background process and the synchronization with the main thread. It also supports reporting progress of the running tasks.

6.2. Using the AsyncTask class
To use AsyncTask you must subclass it. AsyncTask uses generics and varargs. The parameters are the following AsyncTask <TypeOfVarArgParams , ProgressValue , ResultValue> .

An AsyncTask is started via the execute() method.

The execute() method calls the doInBackground() and the onPostExecute() method.

TypeOfVarArgParams is passed into the doInBackground() method as input, ProgressValue is used for progress information and ResultValue must be returned from doInBackground() method and is passed to onPostExecute() as a parameter.

The doInBackground() method contains the coding instruction which should be performed in a background thread. This method runs automatically in a separate Thread.

The onPostExecute() method synchronizes itself again with the user interface thread and allows it to be updated. This method is called by the framework once the doInBackground() method finishes.

6.3. Parallel execution of several AsyncTasks
Android executes AsyncTask tasks before Android 1.6 and again as of Android 3.0 in sequence by default.

You can tell Android to run it in parallel with the usage of the executeOnExecutor() method, specifying AsyncTask.THREAD_POOL_EXECUTOR as first parameter.

The following code snippet demonstrates that.

// ImageLoader extends AsyncTask
ImageLoader imageLoader = new ImageLoader(imageView);

// Execute in parallel
imageLoader.executeOnExecutor(AsyncTask.THREAD_POO L_EXECUTOR, "http://url.com/image.png");
6.4. Disadvantages of using AsyncTasks
The AsyncTask does not handle configuration changes automatically, i.e. if the activity is recreated, the programmer has to handle that in his coding.

A common solution to this is to declare the AsyncTask in a retained headless fragment.