Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android AsyncTask-直到doInBackground完成后才调用onProgressUpdate_Android_Android Asynctask - Fatal编程技术网

Android AsyncTask-直到doInBackground完成后才调用onProgressUpdate

Android AsyncTask-直到doInBackground完成后才调用onProgressUpdate,android,android-asynctask,Android,Android Asynctask,我已经搜索了其他帖子,但没有找到任何描述我具体问题的帖子。他们中的许多人提到从doInBackground调用publishProgress。我从onPreExecute调用publishProgress,希望显示一个对话框,通知用户正在获取数据。正在从从微调器中进行选择的用户启动AsyncTask。在异步任务完成之前,UI将冻结,而不是显示对话框。我在日志消息中添加了一些消息,看起来尽管在onPreExecute中调用了publishProgress,但在doInBackground完成之前,

我已经搜索了其他帖子,但没有找到任何描述我具体问题的帖子。他们中的许多人提到从doInBackground调用publishProgress。我从onPreExecute调用publishProgress,希望显示一个对话框,通知用户正在获取数据。正在从从微调器中进行选择的用户启动AsyncTask。在异步任务完成之前,UI将冻结,而不是显示对话框。我在日志消息中添加了一些消息,看起来尽管在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>

我认为这可能与从微调器选择执行AsyncTask有关,因为只在活动中执行它而不将其连接到微调器不会有这个问题。任何帮助都将不胜感激。

您不会一直显示进度更新对话框,而是在PreExecute上显示,在ProgressUpdate上更新,在PostExecute或cancel上隐藏。在您的情况下,您不需要发布进度,因为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后对话框仍会显示,并立即被取消。