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
Use single thread executor to sent your data sequentially. Do not create new thread for each data transfer.
All Runnables will be executed one by one.