anu saini
anu saini

Reputation: 102

update UI 16 times in a second android

i am getting data packets at the rate of 16 packets per second so after receiving each packet i need to update my UI but after getting around 40-50 packets , UI stucks completely Already tries putting in async task but not helping. in MAinActivity.java i am receiving packets with broadcast receiver and then received location method is called to update the UI

private BroadcastReceiver packetCallback = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                String packetType = (String) intent.getSerializableExtra(
                     PocketLinkFacade.EXTRA_PACKET_TYPE);

                DataPacketModel dataPacket;

                switch (packetType) {
                    case PocketLinkFacade.GOT_NEW_PACKET:
                        dataPacket = (DataPacketModel) intent.getSerializableExtra(
                                PocketLinkFacade.EXTRA_DATAPACKET);

                        receivedLocationPacket(dataPacket);  // method to update UI
                        break;

                }
            }
        }; 

Please help with right approach to update UI and same fast rate so that it doesnt freeze the app

Upvotes: 0

Views: 86

Answers (1)

Alireza Sharifi
Alireza Sharifi

Reputation: 1162

you can use these methods to avoid freezing context.runOnUIThread(Runable); MyView.post(Runable); or just ignore the data and just update your view every second; use a thread with infinite loop and sleep of 1000 ms and put a bool flag in it and on receiving data just check the flag that 1 second is passed to update the UI and turn the flag back. anyway user cannot read all the data less than a second so basically this much updating-data is not usable to user.

Upvotes: 2

Related Questions