2012-10-11 21 views
5

DoInBackground İçinde uygulama bağlamına veya bir etkinliğe başvurmalıyım.AsyncTask doInBackground parametreleri vs yapıcı parametreleri

new MyAsyncTask(getApplicationContext()) ve doInBackground(Context... params) arasındaki iplik güvenliği ve diğer olası çok dişli kavramlar, kısıtlamalar açısından bir fark var mı?

Teşekkürler.

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 
    } 
} 

Sonra wrt Bağlam kendisi çoklu iş parçacığı ilgili hiçbir içsel sorunları vardır: sizi varsayarsak

+0

farkı anlatmak, Rahul @ http://developer.android.com/reference/android/os/AsyncTask.html –

+0

bakınız – midnight

cevap

1

sayılı böyle bir şey var.

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