Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Handler running more often than it should? (postDelayed)

  • 11-01-2013 3:40pm
    #1
    Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,110 Mod ✭✭✭✭


    I am connecting to a terminal emulator using a library in android, this connects to a serial device (a switch) and shows me sent/received data.

    When I receive data the method below is automatically run. When I receive data I wanted to update the emulator screen using invalidate in the onDataReceived method, however this was not working for some reason and so I created a handler to do it every 1 second instead and that works.
    public void onDataReceived(int id, byte[] data) 
    {
        dataReceived = new String(data);
        ((MyBAIsWrapper) bis).renew(data);
        mSession.write(dataReceived);
    
        mSession.notifyUpdate();
        viewHandler.post(updateView);
    }
    

    Now I wanted to test the data received for certain characters so I made a for loop in onDataReceived, again this didn't work -reliably, it would only see the character now and again, missing some of them. I tried to put the for loop in a thread...then it never returned anything at all. So I added the loop to the handler instead:
    Handler viewHandler = new Handler();
    Runnable updateView = new Runnable()
    {
        @Override
        public void run() 
     {
            //update screen ever 1000ms
            mEmulatorView.invalidate();
    
        //should check data received every 1000ms
        for(int i = 0; i < dataReceived.length(); i++)
            {           
                if(dataReceived.charAt(i) == '>')
                    {
    
                         Log.d(TAG, "found >");
                    }
    
                if(dataReceived.charAt(i) == '#')
                    {
    
                         Log.d(TAG, "found #");     
                    }
            }
            viewHandler.postDelayed(updateView, 1000);
      }
    
    };
    

    My problem is that while I can see the screen gets updated once per second, in the logs I can see I find the characters much more frequently, it is printing them out to the log 100s of times really fast, why is this?

    I think the runnable is gone mental so I set up global boolean, so that when I receive data in onDataRecieved it is set to true and the for loop in the handler is run, which sets the boolean to false again. Now it works better than I had originally intended, if the character is found it finds it that one time, and doesn't run once every second checking.
    Handler viewHandler = new Handler();
    	Runnable updateView = new Runnable() {
    		@Override
    		public void run() {
    			mEmulatorView.invalidate();
    			
    			if (statusBool == true)
    			{
    for(int i = 0; i < dataReceived.length(); i++){
    				
    				if(dataReceived.charAt(i) == '>'){
    					
    					Log.d(TAG, "found >");
    					
    				}
    				if(dataReceived.charAt(i) == '#'){
    					
    					Log.d(TAG, "found #");
    					
    				}
    		}
    			statusBool = false;
    			viewHandler.postDelayed(updateView, 1000);
    			
    			}
    		}
    	};
    
    public void onDataReceived(int id, byte[] data) {
    	
    		dataReceived = new String(data);
    		statusBool = true;
    		Log.d(TAG, "in data received " + dataReceived);
    		((MyBAIsWrapper) bis).renew(data);
    
    		mSession.write(dataReceived);
    	        mSession.notifyUpdate();
    	        viewHandler.post(updateView);
    

    What I am confused about is why does the for loop in onDataReceived not work reliably, but setting the boolean does. Similarly invalidate didnt work reliably but calling the handler or writing to the terminal does - invalidate wouldn't run every time data was received, so I had to put it in the handler too. Some things in the method run every time data is received, some do not. The other thing is I am not sure why when I was checking for the characters once per second it was actually printing it out hundreds of times.

    Everything works fine now, I'm just wondering.


Advertisement