2012-10-11 19 views
5

DoInBackground interno Necesito consultar el contexto de la aplicación o una actividad.Parámetros de asInncTask doInBackground frente a los parámetros del constructor

¿Hay alguna diferencia entre new MyAsyncTask(getApplicationContext()) y doInBackground(Context... params) en términos de seguridad de la rosca y otros posibles conceptos de múltiples hilos, restricciones?

Gracias.

+0

consulte http://developer.android.com/reference/android/os/AsyncTask.html –

+0

@ Rahul, la diferencia – midnight

Respuesta

1

Nº Asumiendo que tiene algo como:

private class MyAsyncTask extends AsyncTask<Context, Integer, Long> { 
    private Context _context = null; 

    MyAsyncTask(Context context) { 
    _context = context; 
    } 

    protected Long doInBackground(Context... context) { 
    // if _context and context are the same, it doesn't matter 
    // which you use. 
    return 0; 
    } 

    protected void onProgressUpdate(Integer... progress) { 
    // update progress 
    } 

    protected void onPostExecute(Long result) { 
    // publish result 
    } 
} 

Entonces no hay problemas inherentes con respecto a WRT multi-threading el propio contexto.

Context useMe = getApplicationContext(); 
MyAsyncTask task = new MyAsyncTask(useMe); 
task.execute(useMe); // should use this if provided, otherwise, what was in constructor 
Cuestiones relacionadas