AsyncTask - 在doInBackground完成之后才调用onProgressUpdate

时间:2013-10-21 07:01:26

标签: android android-asynctask

我搜索了其他帖子,但没有找到任何描述我的特定问题。其中许多都是指从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();
    }

}
}

The Spinner:

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就没有这个问题。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您不会继续显示有关进度更新的对话框,而是显示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();       
    }