0

我正在开发安卓应用程序。我需要从 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) {
        }
    }
4

1 回答 1

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();


    }
于 2013-06-22T08:15:40.303 回答