1

我想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();
}


}
4

2 回答 2

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.
}
于 2013-06-27T11:34:34.243 回答
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);
于 2013-06-27T11:40:35.860 回答