如何从AsyncTask的回调函数访问父类的看法?

如何从AsyncTask的回调函数访问父类的看法?

问题描述:

下面是我的示例code。

Here is my sample code.

public class test extends Activity {
  private TextView tv;

  public class Blap extends AsyncTask<String,String,Boolean>{
    private test parent;
    @Override
    protected Boolean doInBackground(String... options) {
        this.auth(options[0],options[1]);
        parent.aresponse(res);
        return true;
    }
    public Blap(test a){
        this.parent = a;
    }
    public auth(String login, String password){
        //process auth
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    //work...
}

public void buttonclick(View v){
    tv = new TextView(this);
    new Blap(this).execute(editText1.getText().toString(),editText2.getText().toString());
    setContentView(tv);
}
public void aresponse(String rs){
    tv.setText(rs);
}
}

但是,当aresponse被调用时,我得到了一个例外:只有创建视图层次可以触摸其观点原来的线程。
我如何正确地等待异步操作完成,并继续处理所需的操作?

But when aresponse is called, I got an exception: only the original thread that created a view hierarchy can touch its views. How do I correctly wait for async operation complete and continue processing needed operations?

的AsyncTask 提供两种posibilites更新UI:

AsyncTask offers you two posibilites to update the UI :

如果您想更新与 doInBackground()(如更新进度$ C $执行任务并行的UI C>),你必须调用 publishProgress() doInBackground()方法内。然后,你必须以更新 onProgressUpdate()法的用户界面。

If you want to update the UI in parallel with the task executed in doInBackground() (e.g. to update a ProgressBar), you'll have to call publishProgress() inside the doInBackground() method. Then you have to update the UI in the onProgressUpdate() method.

如果您想更新UI当任务完成后,你必须这样做,在 onPostExecute()方法。

If you want to update the UI when the task is done, you have to do it in the onPostExecute() method.

/** This method runs on another thread than the UI thread */
@Override
protected String doInBackground(String... _params) {
    for (int progressValue = 0; progressValue  < 100; progressValue++) {
        publishProgress(progressValue);
    }
}

/** This method runs on the UI thread */
@Override
protected void onProgressUpdate(Integer... progressValue) {
    // TODO Update your ProgressBar here
}

/**
 * Called after doInBackground() method
 * This method runs on the UI thread
 */
@Override
protected void onPostExecute(Boolean _result) {
   // TODO Update the UI thread with the final result
}

在你的情况,你应该使用的AsyncTask&LT;字符串,字符串,字符串&GT; ,因为你需要 doInBackground()返回一个字符串。然后,你只需要在治疗 onPostExecute结果()

In your case, you should use an AsyncTask<String, String, String> because you need doInBackground() to return a String. Then you just have to treat the result in onPostExecute()

public class test extends Activity {
    private TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //work...
    }

    public void buttonclick(View v){
        tv = new TextView(this);
        new Blap().execute(editText1.getText().toString(), editText2.getText().toString());
        setContentView(tv);
    }

    public class Blap extends AsyncTask<String, String, String> {
        private test parent;

        @Override
        protected String doInBackground(String... options) {
            this.auth(options[0], options[1]);
            String result = "my result";
            return result;
        }

        public auth(String login, String password){
            //process auth
        }

        @Override
        protected void onPostExecute(String _result) {
           tv.setText(_result);
        }
    }
}