开发者

How to solve UI hanging in Android using asynchronous classes?

开发者 https://www.devze.com 2023-03-14 05:46 出处:网络
I am implementing chat related application. In this application running two methods in asynchronous class, both running simultaneous refresh message and message list this two methods. I am using first

I am implementing chat related application. In this application running two methods in asynchronous class, both running simultaneous refresh message and message list this two methods. I am using first initially load list this refresh method call every 10 sec, both are implementing individual asynchronous class but this time there is some time screen hanging.

How can resolve the screen hanging issue?

This below method calling every 10 seconds:

 public class LatestMessage extends AsyncTask 
     {
     handlerreferesh("",0);
    }

    class RefreshHandler extends Handler 
    {
     public void handleMessage(Message msg)
         {
            Shout1.this.updateUI();
         }
        
        public void sleep(long delayMillis) {
            this.removeMessages(0);
         // if(isUpdateUI  )
       if(!isFinishing())
       {
          sendMessageDelayed(obtainMessage(0), delayMillis);
       }
         }
         };
     private void updateUI(){
     try
     {
      //this below handlerrefresh method implemnting asyncronous class.........
       handlerreferesh("",0);
       mRedrawHandler.sleep(10000);
     }
    catch (Exception e) {
        e.printStackTrace();
         }
         finally
         {
         System.gc();
         System.runFinalization();
         }
         }

second method:

 handlerMessage("",0);
    
    private void handlerMessage(final String messageType, final int dis) 
    {
     new 开发者_运维问答DownLoanPhoto().execute(null);
    }


You can do whatever you want asynchronously, but EVERYTHING that updates UI must be run in UI Thread. So you should run that operations inside a Runnable with runOnUiThread function.

0

精彩评论

暂无评论...
验证码 换一张
取 消