-1
Je travaille mon premier projet gestionnaire de téléchargement Android comment utiliser la tâche async pour fil Muti téléchargement comme ADM appComment utiliser la tâche async pour le téléchargement
Je travaille mon premier projet gestionnaire de téléchargement Android comment utiliser la tâche async pour fil Muti téléchargement comme ADM appComment utiliser la tâche async pour le téléchargement
Vous utilisez cette classe et la fonction de rappel est votre activité (si nécessaire)
public class TaskDownloader extends Fragment {
private static final String TAG = TaskDownloader.class.getSimpleName();
private static final boolean DEBUG = true; // Set this to false to disable logs.
/**
* Callback interface through which the fragment can report the task's
* progress and results back to the Activity.
*/
public static interface TaskCallbacks {
void onPreExecute();
void onProgressUpdate(int percent);
void onCancelled();
void onPostExecute(boolean result);
}
private TaskCallbacks mCallbacks;
private DownloadFileTask mTask;
private boolean mRunning;
/**
* Hold a reference to the parent Activity so we can report the task's current
* progress and results. The Android framework will pass us a reference to the
* newly created Activity after each configuration change.
*/
@Override
public void onAttach(Activity activity) {
if (DEBUG) Log.i(TAG, "onAttach(Activity)");
super.onAttach(activity);
if (!(activity instanceof TaskCallbacks)) {
throw new IllegalStateException("Activity must implement the TaskCallbacks interface.");
}
// Hold a reference to the parent Activity so we can report back the task's
// current progress and results.
mCallbacks = (TaskCallbacks) activity;
}
/**
* This method is called once when the Fragment is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
if (DEBUG) Log.i(TAG, "onCreate(Bundle)");
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
/**
* Note that this method is <em>not</em> called when the Fragment is being
* retained across Activity instances. It will, however, be called when its
* parent Activity is being destroyed for good (such as when the user clicks
* the back button, etc.).
*/
@Override
public void onDestroy() {
if (DEBUG) Log.i(TAG, "onDestroy()");
super.onDestroy();
cancel();
}
/*****************************/
/***** TASK FRAGMENT API *****/
/*****************************/
/**
* Start the background task.
*/
public void start() {
if (!mRunning) {
mTask = new DownloadّFileTask();
mTask.execute();
mRunning = true;
}
}
/**
* Cancel the background task.
*/
public void cancel() {
if (mRunning) {
mTask.cancel(false);
mTask = null;
mRunning = false;
}
}
/**
* Returns the current state of the background task.
*/
public boolean isRunning() {
return mRunning;
}
/***************************/
/***** BACKGROUND TASK *****/
/**
* ***********************
*/
private class DownloadFileTask extends AsyncTask<Void, String, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
if (mCallbacks != null)
mCallbacks.onPreExecute();
}
@Override
protected Boolean doInBackground(Void... voids) {
//downloadFile
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
if (mCallbacks != null)
mCallbacks.onProgressUpdate(Integer.parseInt(values[0]));
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (mCallbacks != null)
mCallbacks.onPostExecute(result);
mRunning = false;
}
@Override
protected void onCancelled() {
super.onCancelled();
if (mCallbacks != null)
mCallbacks.onCancelled();
mRunning = false;
}
}
/************************/
/***** LOGS & STUFF *****/
/**
* ********************
*/
@Override
public void onActivityCreated(Bundle savedInstanceState) {
if (DEBUG) Log.i(TAG, "onActivityCreated(Bundle)");
super.onActivityCreated(savedInstanceState);
}
@Override
public void onStart() {
if (DEBUG) Log.i(TAG, "onStart()");
super.onStart();
}
@Override
public void onResume() {
if (DEBUG) Log.i(TAG, "onResume()");
super.onResume();
}
@Override
public void onPause() {
if (DEBUG) Log.i(TAG, "onPause()");
super.onPause();
}
@Override
public void onStop() {
if (DEBUG) Log.i(TAG, "onStop()");
super.onStop();
}
}
Et votre question étant ...? – Ian
Si vous lisez environ 50 pages de ce site avec la balise Android, vous trouverez dix exemples. – greenapps
essayer ce lien http://stackoverflow.com/questions/tagged/android+android-asynctask?sort=votes&pageSize=50 – kishorepatel