Using ASYNC Task to download map contacts

I managed to add cards to my map overlay, but it slowly loads, and moving around the map is listless. I think this is when he downloads maps from the server. The pins have a size of about 20-30 kb, but sometimes 25 pins can be added. I already tried adding pins in the background, but this will break the application. What can be done to add contacts in the background without affecting performance?

I get the location from the map when the user touches the map and performs this ASYNC task.

private class mapStations extends AsyncTask<Void, Void, JSONObject> { @Override protected JSONObject doInBackground(Void... arg0) { JSONObject obj = null; try { obj = new JSONObject(API.nearByStations(pxLat, pxLng, 0)); } catch (JSONException e) { e.printStackTrace(); } return obj; } @Override protected void onPostExecute(JSONObject details) { String tag = "mapStations"; JSONArray stations; Drawable d = null; try { mapOverlays = mapView.getOverlays(); mapOverlays.clear(); stations = details.getJSONArray("stations"); for (int j = 0; j < stations.length(); j++) { JSONObject jsonObject = stations.getJSONObject(j); Log.i(tag, "url: " + jsonObject.getString("logo")); try { Bitmap staticImage = BitmapFactory .decodeStream((InputStream) new URL(jsonObject.getString("logo")) .getContent()); d = new BitmapDrawable(staticImage); } catch (MalformedURLException e) { Log.e(tag, e.toString()); e.printStackTrace(); } catch (IOException e) { Log.e(tag, e.toString()); e.printStackTrace(); } add(jsonObject.getDouble("lat"), jsonObject.getDouble("lng"), d); } } catch (JSONException e) { e.printStackTrace(); } } } public void add(double lat, double lng, Drawable d) { mapOverlays = mapView.getOverlays(); if(d.equals(null)){ Log.i(tag, "d was null"); d = this.getResources().getDrawable(R.drawable.androidmarker); } itemizedOverlay = new MapsOverlay(d); GeoPoint point = new GeoPoint((int) (lat * 1e6), (int) (lng * 1e6)); OverlayItem overlayitem = new OverlayItem(point, "", ""); itemizedOverlay.addOverlay(overlayitem); mapOverlays.add(itemizedOverlay); } 

This is the trap of adding points to the doinbackground. This does not happen every time, but it happens. I can't seem to reproduce when that happens. Sometimes it happens when I zoom out, sometimes it happens when I move around the map. I had a similar question about this here Android MapView JSON Array Adding an array of points , but I moved it to post execute. As I mentioned earlier, it will take some time to download contacts. I kind of can't write. I have been working on this all day.

  [12-23 13:46:27.246: E/AndroidRuntime(1359): Uncaught handler: thread main exiting due to uncaught exception 12-23 13:46:27.286: E/AndroidRuntime(1359): java.util.ConcurrentModificationException 12-23 13:46:27.286: E/AndroidRuntime(1359): at java.util.AbstractList$SimpleListIterator.next(AbstractList.java:64) 12-23 13:46:27.286: E/AndroidRuntime(1359): at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:41) 12-23 13:46:27.286: E/AndroidRuntime(1359): at com.google.android.maps.MapView.onDraw(MapView.java:476) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.View.draw(View.java:6535) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.drawChild(ViewGroup.java:1531) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.View.draw(View.java:6538) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.widget.FrameLayout.draw(FrameLayout.java:352) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.drawChild(ViewGroup.java:1531) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.View.draw(View.java:6538) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.widget.FrameLayout.draw(FrameLayout.java:352)][1] 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.drawChild(ViewGroup.java:1531) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.drawChild(ViewGroup.java:1529) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.View.draw(View.java:6538) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.widget.FrameLayout.draw(FrameLayout.java:352) 12-23 13:46:27.286: E/AndroidRuntime(1359): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewRoot.draw(ViewRoot.java:1349) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewRoot.performTraversals(ViewRoot.java:1114) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.os.Handler.dispatchMessage(Handler.java:99) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.os.Looper.loop(Looper.java:123) 12-23 13:46:27.286: E/AndroidRuntime(1359): at android.app.ActivityThread.main(ActivityThread.java:4363) 12-23 13:46:27.286: E/AndroidRuntime(1359): at java.lang.reflect.Method.invokeNative(Native Method) 12-23 13:46:27.286: E/AndroidRuntime(1359): at java.lang.reflect.Method.invoke(Method.java:521) 12-23 13:46:27.286: E/AndroidRuntime(1359): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 12-23 13:46:27.286: E/AndroidRuntime(1359): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 12-23 13:46:27.286: E/AndroidRuntime(1359): at dalvik.system.NativeStart.main(Native Method) 
+7
source share
3 answers

To avoid inadvertently editing the list when it is already being edited. I decided to put the dialog in onPreExecute() while the pins are loading. This prevents the user from touching the card when adding new contacts. Until I find time to come up with a better idea on how to prevent this, this will have to be done.

I clear the overlays from the map before starting to add new contacts, this ends during onPreExecute() . On onPostExecute() I reject the dialog and invalidate() card. This will add new cards to the card after adding them to the list.

 @Override protected void onPreExecute() { Log.i(tag, "AsyncTask Started"); dialog.show(); mapOverlays = mapView.getOverlays(); mapOverlays.clear(); } @Override protected void onPostExecute(Void args) { mapView.invalidate(); dialog.dismiss(); Log.i(tag, "AsyncTask Post"); } 
+2
source

ConcurrentModificationException usually occurs when you try to modify a list during iteration. To overcome this exception, you can use the special CopyOnWriteArrayList list type . It safely allows you to add and remove items from the list during iteration. Hope this helps.

+1
source

Your application moves slowly because the onPostExecute function is executed in the user interface thread. The bitmap decoding that you do here causes the user interface to work slowly because it works intensively on the network and you do it in the user interface thread. I would suggest getting all the bitmaps in the doInBackground stream, and then actually adding them to the map using the onPostExecute method. The only thing you have to do in the user interface thread is the add method.

+1
source

All Articles