在我的应用程序中,我有 12 位字节数组,用于读取状态。写完这个数组后,我得到了不同按钮的状态。基于此响应,我将按钮设置为 ON|OFF。我正在重复执行此任务。如果我将此间隔时间设置得太少,即 100 毫秒并且过于频繁地按下这些按钮,那么我的应用程序将停止响应。
这是代码片段。
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
getSharedSettings();
fanDimmer1=(ToggleButton)findViewById(R.id.button_fan1);
fanDimmer2=(ToggleButton)findViewById(R.id.button_fan2);
dimmerLight1=(ToggleButton)findViewById(R.id.button_light1);
dimmerLight2=(ToggleButton)findViewById(R.id.button_light2);
fanDimmer1.setOnClickListener(this);
fanDimmer2.setOnClickListener(this);
dimmerLight1.setOnClickListener(this);
dimmerLight2.setOnClickListener(this);
if(ip.equals("") || port.equals(""))
{
new AlertDialog.Builder(MainActivity.this)
.setTitle("Warning !")
.setMessage("Please set IP and PORT first")
.setIcon(android.R.drawable.ic_dialog_alert)
.setNeutralButton("ok", null)
.show();
}
else
{
new Thread(new Runnable()
{
@Override
public void run()
{
Log.v(TAG, "openconnection");
openConnection();
}
}).start();
m_handler = new Handler();
startRepeatingTask();
}
}
public void openConnection()
{
// TODO Auto-generated method stub
try
{
s = new Socket(ip, Integer.parseInt(port));
i = s.getInputStream();
iD = new DataInputStream(i);
o = s.getOutputStream();
oD = new DataOutputStream(o);
Log.v(TAG, "openconnection 2");
}
catch (UnknownHostException e) {
// TODO: handle exception
Log.v("UnknowHostException :::::", "In Catch Block");
e.printStackTrace();
}
catch (IOException e) {
// TODO: handle exception
Log.v("IOException :::::", "In Catch Block");
e.printStackTrace();
}
}
Runnable m_statusChecker = new Runnable()
{
@Override
public void run()
{
updateStatus();
m_handler.postDelayed(m_statusChecker,100);
}
private void updateStatus()
{
// TODO Auto-generated method stub
Log.v("test", "1");
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
};
o.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);
txt_status.setText(status);
Log.v(TAG, "status is ::"+status);
char[] c=result.toCharArray();
int count=0;
for (int i=0;i<result.length();i++)
{
count++;
char j=c[i];
//Log.v(TAG, count+"::"+j);
if(count==1)
toggleButton=dimmerLight1;
else if(count==2)
toggleButton=fanDimmer2;
else if(count==3)
toggleButton=fanDimmer1;
else if(count==4)
Log.v(TAG, "Count 4 is 0");
if(j=='1')
toggleButton.setChecked(true);
else
toggleButton.setChecked(false);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
void startRepeatingTask() {
m_statusChecker.run();
}
void stopRepeatingTask() {
m_handler.removeCallbacks(m_statusChecker);
}
在按钮点击我这样做:
@Override
public void onClick(View v)
{
if(v.equals(fanDimmer1))
{
if (fanDimmer1.isChecked())
{
Toast.makeText(MainActivity.this, "Fan 1 is ON", Toast.LENGTH_SHORT).show();
setByteArray((byte) 0x01, (byte) 0xff);
} else
{
Toast.makeText(MainActivity.this, "Fan 1 is OFF", Toast.LENGTH_SHORT).show();
setByteArray((byte) 0x01, (byte) 0x00);
}
}
}
这是setByteArray()方法,我有 24 个这种类型的按钮。
任何意见和建议将不胜感激。
谢谢