简体   繁体   中英

AsyncTask refreshing google map api v2

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)

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);

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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