我有这个代码,我有2个AsyncTask,我通过按钮调用它,我还有一个我想问的是,当打开活动时有没有办法自动调用Asynctask?
我尝试使用onCreate,但它是错误
public class SingleCompleteOrder extends ActionBarActivity {
private static final String comp_id = "complete_id";
private static final String comp_table = "complete_table";
private static final String comp_items = "complete_items";
private static final String comp_date = "complete_date";
private TextView oid;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_contact);
...
...
...
...
...
}
public void huss() {
Updatsituation task = new Updatsituation ();
task.execute();
}
private class Updatsituation extends AsyncTask<Void,Void,Boolean> {
private ProgressDialog pd;
private Boolean result = false;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = new ProgressDialog(SingleCompleteOrder.this);
pd.setMessage("Processing commend,Please wait");
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setCancelable(false);
pd.show();
}
@Override
protected Boolean doInBackground(Void... params) {
// TODO Auto-generated method stub
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(Utils.WS_UPDATE_SITUAT);
List<NameValuePair> mParams = new ArrayList<NameValuePair>();
mParams.add(new BasicNameValuePair("complete_id", oid.getText().toString()));
mParams.add(new BasicNameValuePair("order_situation","showed"));
try {
post.setEntity(new UrlEncodedFormEntity(mParams));
HttpResponse response = client.execute(post);
String res = EntityUtils.toString(response.getEntity());
Log.d("Order Preview", res);
JSONObject jsonObj = new JSONObject(res);
result = jsonObj.getBoolean("Result");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(Boolean res) {
// TODO Auto-generated method stub
super.onPostExecute(res);
if (pd != null)
pd.dismiss();
if (res) {
Toast.makeText(SingleCompleteOrder.this,
"Order Completed Successfully", Toast.LENGTH_SHORT)
.show();
finish();
Intent intent = new Intent(SingleCompleteOrder.this,
CompleteOrdertest.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else {
Toast.makeText(SingleCompleteOrder.this,
"Unable to process the command", Toast.LENGTH_SHORT)
.show();
}
}
}
}
答案 0 :(得分:0)
如果要在创建活动时运行Async任务,可以使用OnCreate
方法执行此操作。或者如果您希望每次活动变为可见时执行此操作,您都可以在onResume
方法中运行它。
我没有看到您在任何OnCreate
或OnResume
方法中调用huss()方法。
编辑:试试这个:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_contact);
huss();
...
...
...
...
...
}
答案 1 :(得分:0)
如果要调用AsyncTask,只需在Activity的onCreate()方法中执行以下操作:
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// All lines of code
new Updatsituation().execute();
}
这样就可以启动任务,而无需点击任何按钮。
你可以把它放在onResume()中,但是每次你的活动恢复时都要准备好它。
答案 2 :(得分:0)
如果要自动调用AsyncTask,则需要在OnCreate()方法上调用它。
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
new Updatsituation().execute(); //call your async task
}