Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9253801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:18:35+00:00 2026-06-18T11:18:35+00:00

I am developing an application in which i am using number of ToggleButton .on

  • 0

I am developing an application in which i am using number of ToggleButton.on ON and OFF action i am performing different action. its home Automation based application so buttons are for AC,TV,Light etc. i have fixed byte array to ON and OFF this equipment. i am writing and reading this byte array using socket. but my problem is when user presses two button simultaneously then the button status are gets blinked and sometimes its showing me a ANR dialogues.

Here is my code snippet:

@Override
public void onClick(View v) 
{
    if(v.equals(fanDimmer1))
    {
        if (fanDimmer1.isChecked()) {
        setByteArray((byte) 0x01, (byte) 0xff);
        } else {
        setByteArray((byte) 0x01, (byte) 0x00);
       }
    }
    else if(v.equals(fanDimmer2))
    {
        if (fanDimmer2.isChecked()) {
        setByteArray((byte) 0x02, (byte) 0xff);
        } else {
        setByteArray((byte) 0x02, (byte) 0x00);
     }
    } 
}

And here is the SetByteArray() method.

private synchronized void setByteArray(final byte a, final byte b) 
    {
        new Thread (new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {

                    byte[] data2 = new byte[1024], packet2 =
                        { 
                            (byte) 0x00,(byte) 0x00,(byte) 0x00,
                            (byte) 0x00,(byte) 0x00,(byte) 0x06,
                            (byte) 0x01,(byte) 0x05,(byte) 0x00, 
                             a, b,(byte) 0x00 
                        };

                    //o.write(packet2);
                     write(packet2);
                    i.read(data2, 0, 1024);
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start(); 
    }

Update:

here is the write() method

public synchronized  void write(byte[] pkg)
{
       try {
            o.write(pkg);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     }

initializations:

s = new Socket(ip, Integer.parseInt(port));
i = s.getInputStream();
o = s.getOutputStream();

Update2
this is what i am doing in main thread to set status

Runnable m_statusChecker = new Runnable() 
{
    @Override
    public void run() 
    {
        if (count == 0) {
            updateStatus();
            count = 1;
        } else {
            updateStatus1();
            count = 0;
        }
        m_handler.postDelayed(m_statusChecker,Integer.parseInt(interval));
    }

    private synchronized void updateStatus() 
    {
        // TODO Auto-generated method stub
        try {
            byte[] data1 = new byte[1024], packet1 = 
            { 
                (byte) 0x00,(byte) 0x00,(byte) 0x00, 
                (byte) 0x00,(byte) 0x00,(byte) 0x06, 
                (byte) 0x01,(byte) 0x01,(byte) 0x00,
                (byte) 0x00,(byte) 0x00,(byte) 0x19
            };

            write(packet1);
            i.read(data1, 0, 1024);

            byte_to_hex = ConversionMethods.bytesToHex(data1).substring(18, 26);

            char[] arr = byte_to_hex.toCharArray();
            for (int i = 0; i < arr.length - 1; i += 2) 
            {
                char temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
            }

            swapped_result=new String(arr);
            result = ConversionMethods.hexStringToNBitBinary(swapped_result, 32);

            int counter = 0;
            for( int i=0; i<result.length(); i++ ) 
            {
                if( result.charAt(i) == '1' )
                {
                    counter++;        
                }  
            }
            status=Integer.toString(counter);   
            runOnUiThread(updateButtons);

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

updateButtons()

Runnable updateButtons=new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub 
        txt_status.setText(status);
        /*Log.v(TAG, "status is ::"+status);*/
        char[] c=result.toCharArray();
        int count=0;
        for (int i=0;i<24;i++)
        {
            count++;
            char j=c[i];
            Log.v(TAG, count+"::"+j);
            if(count==1)
                toggleButton=dimmerLight1;
            else if(count==2)
                toggleButton=dimmerLight2;
            else if(count==3)
                toggleButton=dimmerLight3;
            else if(count==4)
                toggleButton=dimmerLight4; 
            else if(count==5)
                toggleButton=dimmerLight5;
            ............
            ............

            if(j=='1')
                toggleButton.setChecked(true);
            else
                toggleButton.setChecked(false); 
        }

    }
};

How to deal with this situation. i have 24 this kind of ToggleButtons. if anyone had this issue before then share with me.

Any help and idea will be appreciated.
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:18:36+00:00Added an answer on June 18, 2026 at 11:18 am

    Use single thread executor to sent your data sequentially. Do not create new thread for each data transfer.

    Executor executor = Executors.newSingleThreadExecutor();
    
    private void setByteArray(final byte a, final byte b) {
        executor.execute(new Runnable() { 
            public void run() { 
                // your code here
            } 
        });
    }
    

    All Runnables will be executed one by one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing an application using the WAF (WPF Application Framework) which is based on
I am developing an application in which I am using GET REST call to
I am developing an application in which i am using location service. Basically that
I'm developing an application which draws some charts and I'm using Google Chart. After
I am developing an iPad application by which i am using image context to
I'm developing an Android application which sends content to a server using rest web
I'm developing an interactive MFC application which displays a 3D object using my own
I'm developing a application using Java RMI which enable clients to register topics they
I am developing blackberry application using BlackBerry JDE 5.0.X , in which i am
I am using Netbeans IDE (J2ME) for developing a mobile application, a dictionary which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.