AsyncTask的刷新谷歌地图API V2 [英] AsyncTask refreshing google map api v2

查看:180
本文介绍了AsyncTask的刷新谷歌地图API V2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用程序从远程API一些JSON数据每个标记(一个标记显示真实设备的位置,还有不到10设备上观看)在地图上present,并设置设备状态通过根据某些规则在给定的JSON数据工作改变标记物的颜色。我用的AsyncTask获取JSON数据和更改设备的状态。我保持的ConcurrentHashMap&LT提取的数据,设备,数据> 。所以,我运行一些asynctasks,一个用于每个设备。我还使用自定义的信息窗口(其实自InfoWindowAdapter),显示有关设备的一些更多的数据。首先,我画一个标记,并让他们在地图的HashMap<设备,标记> 。我用一个执行asynctasks之一:

My android application retrieves some json data from remote API for each Marker (a Marker shows the position of a real device, there are less than 10 devices to watch) present on the map, and sets status of a device by changing a color of the marker according to some rule working on a given json data. I use AsyncTask to fetch json data and change a status of a device. I keep fetched data in ConcurrentHashMap<Device, Data>. So, I run a number of asynctasks, one for each device. I also use a custom info window (in fact custom InfoWindowAdapter) to show some more data about device. First I draw a markers and keep them in a map HashMap<Device, Marker>. I execute asynctasks one by one using:

new MyAsyncTask(markerMap).execute(device)

我的自定义InfoWindowAdapter覆盖getInfoContents方法,其中一些由asynctasks数据收集被用来在信息窗口中显示,点击时。
一切工作正常。但现在我想刷新我的标志,每10秒。我曾尝试使用下面的方法来做到这一点:

My custom InfoWindowAdapter overrides getInfoContents method, where some collected by asynctasks data are used to be shown in InfoWindow, when clicked. Everything works fine. But now I want to refresh my markers every 10 sec. I have tried to do it using the following approach:

Timer timer = new Timer();

timer.scheduleAtFixedRate(new TimerTask(){

    @Override
    public void run() {

        new MyAsyncTask(markerMap).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, device1);
        new MyAsyncTask(markerMap).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, device2);

    }}, 0, 10000);

我的问题是,如果它是刷新我的地图左/最好的方法?或者我应该使用Runnable接口,而不是AsyncTask的,和一个Runnable中获取JSON数据更新标记时,将消息发送到处理程序?还有一个问题:我应该使用计时器或ScheduledExecutorService的?我看过一些计算器的讨论,但我不知道最终的建议。谢谢你。

My question is if it is the right/ best way to refresh my map? Or should I use Runnable instead of AsyncTask, and within a Runnable send a message to Handler when fetching json data to update Marker? One more question: should I use Timer or ScheduledExecutorService? I have read some StackOverflow discussions but I dont know the final recommendation. Thanks.

推荐答案

最后,我已经解决了我的问题。在同时,我已经为了使用的ScheduledThreadPoolExecutor 与子类的Runnable (而不是<$ C重写我的计划的一部分$ C>的AsyncTask )。此外,我已经添加了,我重新启动的ScheduledThreadPoolExecutor 使用onResume()方法: mScheduledThreadPoolExecutor.scheduleAtFixedRate(可运行,0,区间,TimeUnit.MILLISECONDS);

Finally I have solved my problem. In mean time I have rewritten part of my program in order to use ScheduledThreadPoolExecutor with subclassed Runnable (instead of Asynctask). In addition I have added onResume() method where I restart ScheduledThreadPoolExecutor using: mScheduledThreadPoolExecutor.scheduleAtFixedRate(runnable, 0, interval, TimeUnit.MILLISECONDS);

这篇关于AsyncTask的刷新谷歌地图API V2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆