图形页面不坏异步任务onProgressUpdated不坏、图形、任务、页面

2023-09-03 23:05:08 作者:请勿从来

我用 OSMdroid图形页面和使用的AsyncTask 类来获得一些数据,我创建覆盖,并尝试重绘每次我收到了味精的时间。

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.

不幸的是,我能够从客户端得到数据,我能在 onProgressUpdated 创建覆盖到,我甚至叫无效(); 但是好像没有什么改变。不知道是什么问题?

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?

下面是我的的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?

推荐答案

终于解决了。我其实是更换而导致这一切的损失对象,并创建了接口的旧的一个新对象,并因此收到接口到旧MapFragment而不是新的MapFragment数据我MapFragment类。得到它解决了,一旦我找到了逻辑分析code。不管怎么说,感谢@kurtzmarc您的支持非常有帮助,直至现在。我将继续同OSMdroid看到任何更多的东西,我来了。

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.