简体   繁体   English

Android-如何将后台AsyncTask与我的主要活动同步

[英]Android - How to synchronize a background AsyncTask with my main activiy

I'm developing an app which connects to a server to download location data in order to add some markers to a Map (Google Map). 我正在开发一个应用程序,该应用程序连接到服务器以下载位置数据,以便向地图(Google地图)添加一些标记。

My problem is that the map is setted before I download my data, so it is completly empty (with no markers). 我的问题是,在我下载数据之前已设置了地图,因此该地图完全是空的(没有标记)。

That data download is performed in another Thread by an AsyncTask and I don't know how to set up my map AFTER all data have been gathered. 数据下载是由AsyncTask在另一个线程中执行的,在收集完所有数据之后,我不知道如何设置地图。

Some code, just in case it helps: 一些代码,以防万一:

HttpGetWorker.java (in charge of get the data): HttpGetWorker.java (负责获取数据):

...

@Override
protected String doInBackground(String... urls) {
    try {
        return process(urls[0]); //Private method to get data
    } catch (IOException ioe) {
        Log.e("HttpGetWorker:", ioe.getLocalizedMessage(), ioe);
        return "Unable to retrieve web page. URL may be invalid.";
    }
}

@Override
protected void onPostExecute(String result) {
    for(AsyncTaskListener<String> listener : listeners) {
        /**
         * Activities which need the server data have to be listeners
         * of this task. this processResult method comes from an own
         * interface(AsyncTaskListener) and it is implemented in my "main"
         * activity (see below)
         */
        listener.processResult(result); 
    }

    progressDialog.dismiss(); //just a progress dialog set in onPreExecute method
}

...

Then the listener of this task, my "main" activity MapsActivity.java : 然后是该任务的侦听器,即我的“主要”活动MapsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    initData(); //Here it is where I get the server Data, complete method below

    setUpMapIfNeeded(); // These method set up my map, but before my data is retrieved,
    mMap.setOnMarkerClickListener(this);
    PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
}

private void initData() {
    /**
     * This HttpDispatcher just creates an instance of the previous 
     * HttpGetWorker with its execute() to download the data. The "this" 
     * argument is refered to the current activity to register it in my 
     * listeners list, as said before
     */

    HttpDispatcher dispatcher = new HttpDispatcher(this);
    dispatcher.doGet(this);
}

...

// Interface method, here my net data is process
@Override
public void processResult(String result) {
    //stuff to process the result, it does it well, but too late.
}

Hope I explain myself. 希望我自己解释一下。

Thanks everyone! 感谢大家!

EDIT : Relating to the specific problem, sometimes the easiest solutions are the hardest to see...or my head was just a mess when I posted this. 编辑 :关于特定的问题,有时最简单的解决方案是最难看到的...或者当我发布此消息时,我的头只是一团糟。 I just change an addMarkers() method to my proccessResult(String s), after all data have been fetched... 在获取所有数据之后,我只是将addMarkers()方法更改为proccessResult(String s)...

Anyway, I think the question itself could be interesting for others (synchronization between an AsyncTask and any other activity which calls it). 无论如何,我认为问题本身对其他人可能很有趣(AsyncTask与调用它的任何其他活动之间的同步)。 So I let it here. 所以我把它放在这里。

Cheers! 干杯!

Don't set the marker before is AsyncTask finished. AsyncTask完成之前不要设置标记。 When task is finished, set marker from onPostExecute() . task完成后,从onPostExecute()设置标记。

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

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