如何在android中使用带有双参数的AsyncTask

时间:2013-06-27 11:21:29

标签: android android-asynctask double

我希望使用double课程获得两个asynctask值,并且应该在此期间显示progress-dialog我该怎么做才有人请帮助我这样做并给我关于这个主题的一些示例链接..

这是我的代码

    public class MyAsyncTask extends AsyncTask<double, int, double>{

        private Activity activity;
        private ProgressDialog progressDialog;

        private double lon;
        private double lat;

public MyAsyncTask(Activity activity, double longitude, double latitute) {
            super();
this.activity = activity;
this.lon = longitude;
this.lat = latitute;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(activity, "Loading", "Loading", true);
}

protected double doInBackground(double... v) {
//do your stuff here
    if(lat==0.0&&lon==0.0)
return null;

}

@Override
protected void onPostExecute(Result result) {
progressDialog.dismiss();
MyAsyncTask task = new MyAsyncTask(MainActivity.this, gps.getLatitude(), gps.getLongitude());
task.execute();
Toast.makeText(activity.getApplicationContext(), "Finished.", 
        Toast.LENGTH_LONG).show();
}


}

2 个答案:

答案 0 :(得分:2)

您可以根据需要传递多个双打。

asyncTask.execute(double1, double2);

然后在Async Task中,你可以通过以下方式访问它们:

private class MyAsyncTask extends AsyncTask<Double, Integer, Result> {
     Result result=null;
     protected Result doInBackground(Double... doubles) {
         // Getting your doubles
         double1 = doubles[0];
         double2 = doubles[1];

         // Triggers the execution of onProgressUpdate()
         publishProgress((int) double1);

         result = new Result();
         result.setLat(/*Your lat value.*/);
         result.setLon(/*Your lon value.*/);
         return result;
     }

     protected void onProgressUpdate(Integer... progress) {
         // Update your UI with the current progress.
     }

     protected void onPostExecute(Result result) {
         Double lat = result.getLatitude();
         Double lon = result.getLongitude();

         // Do anything after doInBackground() is completed
     }
 }

如果这是你想要的,请告诉我。

修改

class Result {
public lat;
public lon;

public Result(){
}

// Your getters and setters will be declared here.
}

答案 1 :(得分:0)

你也可以这样传递..

private class MyAsyncTask extends AsyncTask<Void, Void, Void> {

public MyAsyncTask(int number) {
    super();
    // do stuff
}

// doInBackground() 
// onPostExecute(), ect

}

将此称为如何调用任何其他类...

new MyAsyncTask(number).execute(maybe_other_params);