2012-10-11 5 views
5

内部doInBackgroundアプリケーションコンテキストまたはアクティビティーを参照する必要があります。AsyncTask doInBackgroundパラメーターとコンストラクターパラメーター

new MyAsyncTask(getApplicationContext())doInBackground(Context... params)の間に、スレッドの安全性やその他のマルチスレッドの概念、制限などの点で違いはありますか?

ありがとうございました。あなたのような何かを持っていると仮定すると

+0

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

+0

@ Rahulを参照してください – midnight

答えて

1

番号:

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

はその後マルチスレッドWRTにコンテキスト自体に関しては固有の問題がありません。

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