0

For this code snippet( I have excluded the doInBackground(), postExecute() etc. ) How should I pass the Activity parameter while calling the Async Task from the CheckServer Activity?

public class CheckServer  extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);


    HttpTicket ticket= new HttpTicket(); //HOW IS THIS LINE DONE? WHAT PARAM SHOULD BE PASSED?



    }

    @SuppressWarnings("unused")
    private class HttpTicket extends AsyncTask<String, String, String>
    {
        private Activity activity;
        private ProgressDialog dialog;

         public HttpTicket(Activity activity) {

            this.activity = activity;


        }
4

2 回答 2

0

在你的活动 onCreate()

 HttpTicket ticket= new HttpTicket(Activity.this);
 //passing context to the asynctask constructor
 ticket.execute();
 //call execute to laod asynctask

定义 asynctask 如下

private class HttpTicket extends AsyncTask<String, String, String>
{
    private Activity activity;
    private ProgressDialog dialog;

     public HttpTicket(Activity activity) {

        this.activity = activity;
        dialog = new ProgressDialog(activity);
        dialog.setTitle("Wait...");
    }
   protected void onPreExecute()
   {
      dialog.show();
   }
   protected String doInBackground(String params)
   {  
      //background opearation

     return "string";
   }
   protected void onPostExecute(String result)
   {
       dialog.dismiss();
       //update ui
   }


  }
于 2013-04-05T08:36:20.030 回答
0

你可以简单地做

HttpTicket mHttpTicket = new HttpTicket(this); 
mHttpTicket.execute(); 

您也可以删除构造函数,并将其作为参数传递给 OnPreExecute。然后你给它execute(this);

于 2013-04-05T08:37:00.640 回答