我正在开发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) {
}
}
答案 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();
}