如何从android中的onpostexecute方法获取返回值?

时间:2013-06-22 08:02:56

标签: android

我正在开发Android应用程序。我需要从onpostexecute方法中获取值。即我需要使用onpostexecute方法的返回值执行一些任务。我怎样才能做到这一点?请帮助我。

我的代码:

boolean val= false;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LongOperation1 op = new LongOperation1();
            op.execute("");
//I want the value here again


}

private class LongOperation1 extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {

            loadFeed2();
            return "Executed";
        }
            @Override
        protected void onPostExecute(String result) {
            dialog1.dismiss();
            try {
                     dialog.dismiss();
//mystuff
                val = true;


        }
            catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        @Override
        protected void onPreExecute() {
            dialog1 = ProgressDialog.show(Taketest.this, "Please wait...",
                    "Retrieving data ...", true);
        }

        @Override
        protected void onProgressUpdate(Void... values) {
        }
    }

1 个答案:

答案 0 :(得分:1)

你可以尝试这样的事, 将布尔值设为全局然后

   Public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LongOperation1 op = new LongOperation1();
    op.execute("");
    // here should not check val is true or not because it will run before ansynch complete
}

public void init()
{
 if(val)
      {

   Log.e("val is","true");
      }

 }

 ...........

    @Override
    protected void onPostExecute(String result) {
        dialog1.dismiss();
        try {
                 dialog.dismiss();
     //mystuff
            val = true;

             init();


    }