简体   繁体   English

如何使用异步任务定期保存位置更新?

[英]How to periodically save location updates using async task?

Currently I am trying to collect location updates periodically, say for every 10 samples. 目前,我正在尝试定期收集位置更新,例如每10个样本一次。 I am using an arraylist to collect them and clear off the arraylist in the main UI thread once I passed them to the server using async task. 我正在使用arraylist收集它们,并在使用异步任务将它们传递给服务器后,在主UI线程中清除了arraylist。 In the async task I am loading the arraylist with the one from main UI. 在异步任务中,我正在从主UI加载arraylist。

The problem is, it is clearing the arraylist in the async task even it is in separate variable. 问题是,即使在单独的变量中,它也在清除异步任务中的arraylist。 How can I keep the activity in sync. 如何保持活动同步。 Do I need to sleep the main activity till the async task finishes. 我是否需要休眠主要活动,直到异步任务完成。 I am not sure about the variable. 我不确定该变量。 Can someone explain how to do this? 有人可以解释如何做吗?

MainMapActivity(X){
  locationupdate for every 1 min{
  arraylist a;//this collects all location updates 10 samples each time
  call asynctask b;
  clear a;
}
asynctask b{
  arraylist c = getall from  a;
  db= insert(c);//save a into database;
}

Clearing a in main UI clears of variable c. 在主用户界面中清除a会清除变量c。 How can I prevent that? 我该如何预防? The variable c should be only cleared after saving all the data in it. 仅在将所有数据保存后才清除变量c。

If I'm getting what you are trying to say is,then yes, we have a way to fix your problem using handlers. 如果我想知道的是,那么可以,我们可以使用处理程序来解决您的问题。

In your async task, do something like this - 在你的异步任务,做这样的事情 -

   private mLocations;
 public MyTask(Handler mResponseHandler, List<Location> mLocations){
        super();
        this.mLocations = mLocations;
        this.mResponseHandler = mResponseHandler;
    }

in onPostExecute, 在onPostExecute中,

  onPostExecute(List<Location>){

 @Override
    protected void onPostExecute(Boolean result) {

        super.onPostExecute(result);

        Log.i(TAG, "result = "+result);
        if (mResponseHandler == null) return;

        MyLocationData<Location> resultData = new MyLocationData<Location>();
        if(result != null && result){
            resultData.requestSuccessful = true;
            resultData.responseErrorCode = 0;
        }else{
            resultData.requestSuccessful = false;
            resultData.responseErrorCode = errorCode;  //set this when result is null
        }

        android.os.Message m = android.os.Message.obtain();
        m.obj = resultData;
        mResponseHandler.sendMessage(m);
    }
}

Where MyLocationData is a model class in which I'm saving all relevant data. MyLocationData是其中保存所有相关数据的模型类。 It can be a class like this - 可能是这样的一类-

 public class MyLocationData<Type> {

    public Type response;
    public int responseErrorCode;
    public boolean requestSuccessful;
}

Now, in your activity, you can get this data like, 现在,在您的活动中,您可以获取以下数据,

private Handler mExportHandler = new Handler(){ 

        public void handleMessage(android.os.Message msg) {
                MyLocationData<Location> responseData = (MyLocationData<Location>) msg.obj;
                 // your logic for fetching new locations from responseData and using them in your activity   

        };
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM