6

在 doInBackground 中,我需要引用应用程序上下文或活动。

new MyAsyncTask(getApplicationContext())线程安全和doInBackground(Context... params)其他可能的多线程概念、限制之间有什么区别吗?

谢谢。

4

1 回答 1

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

那么关于上下文本身的多线程就没有固有的问题。

Context useMe = getApplicationContext();
MyAsyncTask task = new MyAsyncTask(useMe);
task.execute(useMe); // should use this if provided, otherwise, what was in constructor
于 2014-11-19T19:25:01.933 回答