【问题标题】:Android Java : What happens in an AsyncTask with more than one task being operated?Android Java:在运行多个任务的 AsyncTask 中会发生什么?
【发布时间】:2016-12-24 06:31:31
【问题描述】:

我想在后台执行两项任务,但我认为最好在后台同时执行它们并在完成后返回地图。

但我的问题是我认为其中一个没有被处理,或者至少我不确定它们是否应该都放在那里。

我的代码如下。

  final TextView         v = (TextView) P.findViewById(R.id.abbr);
  final SimpleDraweeView i = (SimpleDraweeView) P.findViewById(R.id.icon);
  new AsyncTask<Object, Object, HashMap<String, Object>>() {
     @Override
     protected HashMap<String, Object> doInBackground(Object... params) {
        String   k = SyncProfiles.getIcon(A, C.getNumber());
        Drawable dr;
        if (k == null) {
           dr = H.setDrawableColor(A, R.drawable.contact_user_bg, H.aoRandColor());
        } else {
           dr = null;
        }
        HashMap<String, Object> r = new HashMap<>();
        r.put("bg", dr);
        r.put("url", Images.ImageServerResize(k, 180));
        return r;
     }

     @Override
     protected void onPostExecute(HashMap<String, Object> o) {
        v.setText(C.getName().trim().substring(0, 1));
        if (o.get("url") == null) {
           contact_bg.setBackground((Drawable) o.get("bg"));
           i.setVisibility(View.GONE);
        } else {
          i.setImageURI(Uri.parse((String) o.get("url")));
          i.setVisibility(View.VISIBLE);
        }
     }
  }.execute();

【问题讨论】:

  • 第二个AsyncTask在哪里?
  • @ρяσѕρєяK,这是一个AsyncTask,我想在里面做两个操作。

标签: java android concurrency android-asynctask


【解决方案1】:

不要在同一个实例上多次调用 AsyncTask.execute()。

// For example
SampleTask  sampleTask = new SampleTask();  
sampleTask.execute();  
sampleTask.execute(); //Error, IllegalStateException 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多