Android异步任务传递值

Android异步任务传递值,android,android-asynctask,android-dialogfragment,android-location,Android,Android Asynctask,Android Dialogfragment,Android Location,我想获取用户在DialogFragment中的当前位置,并将所有数据传递给SQLite数据库。在我的DoInBackground中,我得到了当前运行良好的位置 我想设置long和lat的TextView,如何将值传递给AysncTask并返回到对话框 protected getLocation doInBackground(String... params) { } 我想在外部类中传递值,以便将数据放入sql中 public Dialog onCreateDialog(Bundle save

我想获取用户在DialogFragment中的当前位置,并将所有数据传递给SQLite数据库。在我的DoInBackground中,我得到了当前运行良好的位置

我想设置long和lat的TextView,如何将值传递给AysncTask并返回到对话框

 protected getLocation doInBackground(String... params) {
}

我想在外部类中传递值,以便将数据放入sql中

public Dialog onCreateDialog(Bundle savedInstanceState){


    final AlertDialog.Builder build = new AlertDialog.Builder(getActivity());

    text = (TextView) view.findViewById(R.id.insert_long);
    text2 = (TextView) view.findViewById(R.id.insert_lat);
    build.setView(view);

    db = new DBHelper(getActivity());
    db.open();
    build.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {


            }
        }
    });

异步任务使用的三种类型如下:

  • Params,执行时发送到任务的参数类型
  • Progress,在过程中发布的进度单位的类型 背景计算
  • 结果,背景计算结果的类型
  • 因此,您需要通过Double[]或自定义结构包装lat,lon作为结果

     private class MyTask extends AsyncTask<Void, Void, Double[]>
    
    私有类MyTask扩展了AsyncTask
    

    并实现
    onPostExecute(Double[]result)
    方法。

    您可以在声明类时指定方法的返回类型
    doInBackground(…)
    。 像这样:

    private class LocationReceiver extends AsyncTask<String, Void, double[]> {
    protected String doInBackground(String... params) {
        try {
            LocationManager loca = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    
            double longitude = location.getLongitude();
            double latitude = location.getLatitude();
            return new double[] {longitude, latitude};
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    }
    
    protected void onPostExecute(double[] resultArray) {
       // Do something with the returned location
       double lon = resultArray[0];
       double lat = resultArray[1];
    }
    
    私有类LocationReceiver扩展异步任务{
    受保护的字符串doInBackground(字符串…参数){
    试一试{
    LocationManager loca=(LocationManager)context.getSystemService(context.LOCATION\u服务);
    double longitude=location.getLongitude();
    双纬度=location.getLatitude();
    返回新的双精度[]{经度,纬度};
    }捕获(例外e){
    //TODO自动生成的捕捉块
    e、 printStackTrace();
    }
    }
    PostExecute上受保护的void(双[]结果数组){
    //对返回的位置执行一些操作
    双lon=resultArray[0];
    双lat=结果数组[1];
    }
    

    }使用纬度和经度变量创建自定义类:

    class LatLng {
       public final  double latitude;
       public final  double longitude;
    
       LatLng(double latitude, double longitude) {
           this.latitude = latitude;
           this.longitude = longitude;
       }
    }
    
    现在从
    doInBackground
    方法返回
    LatLng
    对象,以在
    onPostExecute
    方法中获取两个值:

    protected LatLng doInBackground(String... params) {
            ...
            double longitude = location.getLongitude();
            double latitude = location.getLatitude();
      return new LatLng(location.getLatitude(),location.getLongitude());
    
    }
    
    并将
    LocationTask
    类更改为:

    private static class LocationTask extends AsyncTask<Location,
                                                 Void, getLocation> {
    
        ....
        @Override
        protected getLocation doInBackground(String... params) {  
         // your code here
    
        }
        @Override
        protected void onPostExecute(getLocation result) {
    
            text = (TextView) context.findViewById(R.id.textName);
            text.setText(result.latitude +"--"+result.longitude);
        }
      }
    
    私有静态类LocationTask扩展了AsyncTask{
    ....
    @凌驾
    受保护的getLocation doInBackground(字符串…参数){
    //你的代码在这里
    }
    @凌驾
    受保护的void onPostExecute(getLocation结果){
    text=(TextView)context.findViewById(R.id.textName);
    text.setText(result.latitude+“--”+result.longitude);
    }
    }
    
    我不能100%确定您的要求。是否要从异步任务返回位置?如果是这样,您可以从
    String。。。参数
    位置。。。params
    我想返回long和lat,以便将它们设置为TextView,并通过onClick将它们存储到SQLite中。db.insert()是我的DbHelper中的一个方法。您可以发布如何调用asynctask的代码吗?我已经更新了我的code@Mark-这不是完整的代码,因为我找不到您的
    LocationTask
    execute()上的行以及
    control
    ?我只想简单地获取用户的当前位置,因为它会减慢我的主线程,所以现在我想把Long和lat传递回我的对话框,我更新了我的答案。您还可以创建一个只包含纬度和经度的简单POJO类,我不太清楚understand@user370305:两者都正确使用自定义类或默认LatLng类,而不是返回
    Location
    object@Mark:那么问题是什么?@ρ∑ρєK在DoInBackground获得long和lat后,我希望postexecute将我的TextView设置为long和lat,我如何调用long和lat使其位于我的db中。insert()方法
    private static class LocationTask extends AsyncTask<Location,
                                                 Void, getLocation> {
    
        ....
        @Override
        protected getLocation doInBackground(String... params) {  
         // your code here
    
        }
        @Override
        protected void onPostExecute(getLocation result) {
    
            text = (TextView) context.findViewById(R.id.textName);
            text.setText(result.latitude +"--"+result.longitude);
        }
      }