【问题标题】:AsyncTask - onProgressUpdate not called until after doInBackground completesAsyncTask - 直到 doInBackground 完成后才调用 onProgressUpdate
【发布时间】:2013-10-21 07:01:26
【问题描述】:

我搜索了其他帖子,但没有找到任何描述我的特定问题的帖子。其中许多是指从 doInBackground 调用 publishProgress。我正在从 onPreExecute 调用 publishProgress,期望显示一个对话框以通知用户正在获取数据。 AsyncTask 正在从用户从 Spinner 中进行选择启动。 UI 不会显示对话框,而是冻结直到 AsyncTask 完成。我已在日志消息中添加,看起来虽然在 onPreExecute 中调用了 publishProgress,但直到 doInBackground 完成后才会执行 onProgressUpdate。

活动:

public class MainActivity extends Activity implements OnItemSelectedListener{

private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}



@Override
protected void onResume() {
    super.onResume();

    spinner = (Spinner)findViewById(R.id.spinner);
    SpinnerAdapter adapter = new CustomSpinnerAdapter<String>(this, new String[]{"one", "two", "three"});
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);

}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@SuppressLint("NewApi")
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    try{
        FetchDataTask fetchDataTask = new FetchDataTask();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
            fetchDataTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "data");
        } else{
            fetchDataTask.execute("data");
        }
        fetchDataTask.await();
    } catch (InterruptedException e){
        e.printStackTrace();
    }
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

public class FetchDataTask extends AsyncTask<String, Integer, Boolean> {

    private  final String TAG = FetchDataTask.class.getSimpleName();
    private  final Integer FETCHING_DATA = 1;

    CountDownLatch countDownLatch = new CountDownLatch(1);
    ProgressDialog dialog;

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        Log.d(TAG,"Exiting onPostExecute");     
        dialog.dismiss();       
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        publishProgress(FETCHING_DATA);
        Log.d(TAG, "Exiting onPreExecute");
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        dialog = ProgressDialog.show(MainActivity.this, "Fetching", "Data");
        Log.d(TAG, "Exiting onProgressUpdate");

    }

    @Override
    protected Boolean doInBackground(String... arg0) {

        /*
         *  Fetching data here
         */

        Log.d(TAG, "Exiting doInBackground");
        countDownLatch.countDown();
        return true;
    }

    public void await() throws InterruptedException{
        countDownLatch.await();
    }

}
}

旋转器:

public class CustomSpinnerAdapter<T> implements SpinnerAdapter {

Context context;
T[] values;

public CustomSpinnerAdapter(Context context, T[] values){
    this.context = context;
    this.values = values;
    for(T value: this.values){
        Log.d("Spinner", value.toString());
    }
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return values.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return values[position];
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public int getItemViewType(int position) {
    // TODO Auto-generated method stub
    return android.R.layout.simple_spinner_dropdown_item;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    TextView v = new TextView(context);
    v.setTextColor(Color.BLACK);
    v.setText(values[position].toString());
    return v;
}

@Override
public int getViewTypeCount() {
    // TODO Auto-generated method stub
    return 1;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean isEmpty() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void registerDataSetObserver(DataSetObserver observer) {
    // TODO Auto-generated method stub

}

@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
    // TODO Auto-generated method stub

}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    return this.getView(position, convertView, parent);
}

}

观点:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Spinner
    android:id="@+id/spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     />

</RelativeLayout>

我认为这可能与从 Spinner 选择中执行 AsyncTask 有关,因为仅在 Activity 中执行它而不将其连接到 Spinner 不会出现此问题。任何帮助将不胜感激。

【问题讨论】:

    标签: android android-asynctask


    【解决方案1】:

    您不会继续显示进度更新对话框,而是显示它onPreExecute 更新它onProgressUpdate 并隐藏它onPostExecutecancel。在您的情况下,您不需要发布进度,因为 progressBar 是不确定的

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            Log.d(TAG, "Exiting onPreExecute");
            dialog = ProgressDialog.show(MainActivity.this, "Fetching", "Data");
        }
    
        @Override
        protected Boolean doInBackground(String... arg0)
        {
            Log.d(TAG, "Exiting doInBackground");
            countDownLatch.countDown();
            //publishProgress(x);
            return true;
        }
    
        public void await() throws InterruptedException
        {
            countDownLatch.await();
        }
    
        @Override
        protected void onProgressUpdate(Integer... values)
        {
            super.onProgressUpdate(values);
            Log.d(TAG, "Exiting onProgressUpdate");
            // update progressbar value
            // ex progresssBar.setProgress(values[0]);
    
        }
        @Override
        protected void onPostExecute(Boolean result)
        {
            super.onPostExecute(result);
            Log.d(TAG,"Exiting onPostExecute");     
            dialog.dismiss();       
        }
    

    【讨论】:

    • 这不能解决问题。我确保在 doInBackground 中睡 5 秒。这应该留出很多时间让对话框显示,但在退出 doInBackground 后对话框仍然显示并立即关闭。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 2011-04-06
    相关资源
    最近更新 更多