MapView doesn't invalidate onProgressUpdated of Async Task

淺唱寂寞╮ 提交于 2019-12-13 02:57:34

问题


I'm using OSMdroid Mapview and using AsyncTask class to get some data, and I create overlays and try to redraw every time I get a msg.

Unfortunately I'm able to get data from a client and I'm able to create overlays to in onProgressUpdated, I've even called invalidate(); But nothing seems to happen. Not sure what is the problem?

Here's my AsyncTask:

public class TaskManager extends AsyncTask<Void, GeoPoint, Void>{

    .....

    public TaskManager(Master master,MapView mapview) {
       //Construtor
    }

    @Override
    protected Void doInBackground(Void... arg0) {

        if(Constance.TCPIP) {
            Log.d("APP","Inside TCPIP");
            //Creation of TCPIP Sockets
            try {
                m_ssocket = new ServerSocket(Constance.PORT_NO);
                Log.d("APP","ServerSocket: "+m_ssocket);
                m_socket = m_ssocket.accept();
                Log.d("APP","Accepted: "+m_socket);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else if (Constance.UDPIP) {         

            //Creation of UDP Sockets
            try {
                m_dsocket = new DatagramSocket(Constance.PORT_NO);
            } catch (SocketException e) {
                e.printStackTrace();
            }
        }
        else if (Constance.MCUDP) {

            //Lock Wifi multicast
            mMultiCastLock = new MultiCastLock(mMaster.getBaseContext());
            mMultiCastLock.setMultiCastAcquire();

            //Creation of MC-UDP Sockets
            try {
                m_mcsocket = new MulticastSocket(Constance.PORT_NO);
                InetAddress address = InetAddress.getByName(Constance.GROUP_ADDR);
                m_mcsocket.joinGroup(address);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // Create a buffer to read datagrams into.
        byte[] mSocketbuffer = new byte[Constance.DGRAM_LEN];

        if(Constance.TCPIP) {
            try {
                m_inSocketData = new BufferedReader(new InputStreamReader(m_socket.getInputStream()));
                Log.d("APP","Reading");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {            
            // Create a packet to receive data into the buffer
            m_inPacket = new DatagramPacket(mSocketbuffer, mSocketbuffer.length);
        }

        //prepare overlay items
        prepareItemizedOverlay();

        // Now loop forever, waiting to receive packets and printing them.
        if(m_ssocket!=null || m_dsocket!=null || m_mcsocket!=null)
        while (true) {

        if (isCancelled())  break;

            //Get Data
            parseData();

            //Make Packet Object
            if(mMSG!=null) {
                make(mMSG);
            }

            if(m_inPacket!=null && !Constance.TCPIP) {
                // Reset the length of the packet before reusing it.
                m_inPacket.setLength(mSocketbuffer.length);
            }
        }


        return null;
    }

    @Override
    protected void onProgressUpdate(GeoPoint... geoPoints){

    OverlayItem overlayItem = new OverlayItem("Name", "Description", geoPoints[0]);
            mItemizedOverlay.addOverlay(overlayItem);
            mMapView.getOverlays().add(mItemizedOverlay);
            mMapView.getController().animateTo(geoPoints[0]);

        mMapView.invalidate();  

    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
        if(Constance.TCPIP) {
            if(m_ssocket!=null && m_socket!=null){
                try {
                    m_ssocket.close();
                    m_socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else if(Constance.UDPIP) {
            if(m_dsocket!=null)
                m_dsocket.close();
        } else if(Constance.MCUDP) {
            if(m_mcsocket!=null)
                m_mcsocket.close();
        }
        Log.d("APP","Task Ended");
    }

    private void parseData() {

        if(Constance.TCPIP) {
            // Wait to receive a socket data
            try{
                mMSG = m_inSocketData.readLine();

            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            // Wait to receive a datagram
            try {
                m_dsocket.receive(m_inPacket);
                // Convert the contents to a string, and display them
            } catch (IOException e) {
                e.printStackTrace();
                }
        }       
    }

    private void make(String plot) {


            //Make Object
            mMSG = new MSG(plot);

            //Overlay
            mGeoPoint = mMSG.getGeoPoint();
            publishProgress(mMSG.getGeoPoint());

    }

    private void prepareItemizedOverlay() {
        /*  itemized overlay */
        Drawable newMarker = mMaster.getResources().getDrawable(R.drawable.ic_sensor);
        mItemizedOverlay = new PlotItemOverlay(mMaster,mItemList,newMarker,
              new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {

              @Override
              public boolean onItemSingleTapUp(int index, OverlayItem item) {
                Log.d("APP","HERE");
                return true;
              }

              @Override
              public boolean onItemLongPress(int index, OverlayItem item) {

                  return true;
              }
          }, mResourceProxy);

    }    
}

Everything seems to work, but nothing seems to happen, not sure what is the problem?


回答1:


Finally resolved it. I was actually replacing my MapFragment class which led to all this loss of Object and a new object created was interfacing the old one, and so the data received to interfacing to the older MapFragment and not the new MapFragment. Got it resolved, once I found the logically analyzing the code. Anyways, thanks for the support @kurtzmarc you have been very helpful until now. I will continue same with OSMdroid to see any more things that I come up with.



来源:https://stackoverflow.com/questions/23011264/mapview-doesnt-invalidate-onprogressupdated-of-async-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!