遇到这么一个需求:
app有若干个对象,每个对象分别有多种状态,只要其中某个对象的状态发生变化,就上报给服务器,谁的状态变了,变成什么样了,这些数据都告诉服务器
业务流程图:
设计思路:
1.因为状态是变化时间是未知的,所以开一个子线程一直监听
2.创建两个全局静态的值,用if方法做比对操作,一旦数据变了,就上报服务器
3.数据一旦变了,就赋值给全局的静态值
代码:https://github.com/wrs13634194612/NestStateListener
效果图:
主界面:创建子线程,对比方法,界面效果
package com.example.administrator.testz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
private Thread sThread = null;
private ToggleButton nest_door, drone, drone_charging, drone_power, drone_remote,
drone_charge, drone_live, in_live, out_live, drone_connect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initService();
nest_door = (ToggleButton) findViewById(R.id.nest_door);
drone = (ToggleButton) findViewById(R.id.drone);
drone_charging = (ToggleButton) findViewById(R.id.drone_charging);
drone_power = (ToggleButton) findViewById(R.id.drone_power);
drone_remote = (ToggleButton) findViewById(R.id.drone_remote);
drone_charge = (ToggleButton) findViewById(R.id.drone_charge);
drone_live = (ToggleButton) findViewById(R.id.drone_live);
in_live = (ToggleButton) findViewById(R.id.in_live);
out_live = (ToggleButton) findViewById(R.id.out_live);
drone_connect = (ToggleButton) findViewById(R.id.drone_connect);
nest_door.setOnCheckedChangeListener(this);
drone.setOnCheckedChangeListener(this);
drone_charging.setOnCheckedChangeListener(this);
drone_power.setOnCheckedChangeListener(this);
drone_remote.setOnCheckedChangeListener(this);
drone_charge.setOnCheckedChangeListener(this);
drone_live.setOnCheckedChangeListener(this);
in_live.setOnCheckedChangeListener(this);
out_live.setOnCheckedChangeListener(this);
drone_connect.setOnCheckedChangeListener(this);
}
private void initService() {
sThread = new Thread(sRunnable);
sThread.start();
}
private Runnable sRunnable = new Runnable() {
@Override
public void run() {
//比对方法
while (true) {
compare();
}
}
};
private void compare() {
// 比较音源数据 一直比对 只要数据变了 立马赋值 然后下发
if (DataStruct.SendDeviceData.SYS.nest_door != DataStruct.RcvDeviceData.SYS.nest_door
|| DataStruct.SendDeviceData.SYS.drone != DataStruct.RcvDeviceData.SYS.drone
|| DataStruct.SendDeviceData.SYS.drone_charging != DataStruct.RcvDeviceData.SYS.drone_charging
|| DataStruct.SendDeviceData.SYS.drone_power != DataStruct.RcvDeviceData.SYS.drone_power
|| DataStruct.SendDeviceData.SYS.drone_remote != DataStruct.RcvDeviceData.SYS.drone_remote
|| DataStruct.SendDeviceData.SYS.drone_charge != DataStruct.RcvDeviceData.SYS.drone_charge
|| DataStruct.SendDeviceData.SYS.drone_live != DataStruct.RcvDeviceData.SYS.drone_live
|| DataStruct.SendDeviceData.SYS.in_live != DataStruct.RcvDeviceData.SYS.in_live
|| DataStruct.SendDeviceData.SYS.out_live != DataStruct.RcvDeviceData.SYS.out_live
|| DataStruct.SendDeviceData.SYS.drone_connect != DataStruct.RcvDeviceData.SYS.drone_connect) {
DataStruct.SendDeviceData.SYS.nest_door = DataStruct.RcvDeviceData.SYS.nest_door;
DataStruct.SendDeviceData.SYS.drone = DataStruct.RcvDeviceData.SYS.drone;
DataStruct.SendDeviceData.SYS.drone_charging = DataStruct.RcvDeviceData.SYS.drone_charging;
DataStruct.SendDeviceData.SYS.drone_power = DataStruct.RcvDeviceData.SYS.drone_power;
DataStruct.SendDeviceData.SYS.drone_remote = DataStruct.RcvDeviceData.SYS.drone_remote;
DataStruct.SendDeviceData.SYS.drone_charge = DataStruct.RcvDeviceData.SYS.drone_charge;
DataStruct.SendDeviceData.SYS.drone_live = DataStruct.RcvDeviceData.SYS.drone_live;
DataStruct.SendDeviceData.SYS.in_live = DataStruct.RcvDeviceData.SYS.in_live;
DataStruct.SendDeviceData.SYS.out_live = DataStruct.RcvDeviceData.SYS.out_live;
DataStruct.SendDeviceData.SYS.drone_connect = DataStruct.RcvDeviceData.SYS.drone_connect;
// SendDataToDevice(false); 数据变了 发送tcp信息给服务器
System.out.println("数据变了");
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.nest_door:
System.out.println("机巢门"+isChecked);
DataStruct.RcvDeviceData.SYS.nest_door = isChecked;
break;
case R.id.drone:
System.out.println("无人机在位状态"+isChecked);
DataStruct.RcvDeviceData.SYS.drone = isChecked;
break;
case R.id.drone_charging:
System.out.println("无人机充电状态"+isChecked);
DataStruct.RcvDeviceData.SYS.drone_charging = isChecked;
break;
case R.id.drone_power:
System.out.println("无人机电源状态"+isChecked);
DataStruct.RcvDeviceData.SYS.drone_power = isChecked;
break;
case R.id.drone_remote:
System.out.println("无人机遥控器"+isChecked);
DataStruct.RcvDeviceData.SYS.drone_remote = isChecked;
break;
case R.id.drone_charge:
System.out.println("无人机充电器"+isChecked);
DataStruct.RcvDeviceData.SYS.drone_charge = isChecked;
break;
case R.id.drone_live:
System.out.println("无人机推流状态"+isChecked);
DataStruct.RcvDeviceData.SYS.drone_live = isChecked;
break;
case R.id.in_live:
System.out.println("内部摄像头推流状态"+isChecked);
DataStruct.RcvDeviceData.SYS.in_live = isChecked;
break;
case R.id.out_live:
System.out.println("外部摄像头推流状态"+isChecked);
DataStruct.RcvDeviceData.SYS.out_live = isChecked;
break;
case R.id.drone_connect:
System.out.println("无人机连接状态"+isChecked);
DataStruct.RcvDeviceData.SYS.drone_connect = isChecked;
break;
default:
break;
}
}
}
类的对象1:
package com.example.administrator.testz;
/**
* Created by wrs on 2019/7/26,18:59
* projectName: Testz
* packageName: com.example.administrator.testz
*/
public class DataStruct {
public static Data RcvDeviceData = new Data();
public static Data SendDeviceData = new Data();
}
类的对象2:
package com.example.administrator.testz;
/**
* Created by wrs on 2019/7/26,19:00
* projectName: Testz
* packageName: com.example.administrator.testz
*/
public class Data {
public DataStruct_System SYS = new DataStruct_System();
}
类的对象3:
package com.example.administrator.testz;
/**
* Created by wrs on 2019/7/26,18:40
* projectName: Testz
* packageName: com.example.administrator.testz
*/
public class DataStruct_System {
public boolean nest_door = false; //机巢门
public boolean drone = false; //无人机在位状态
public boolean drone_charging = false; //无人机充电状态
public boolean drone_power = false; //无人机电源状态
public boolean drone_remote = false; //无人机遥控器
public boolean drone_charge = false; //无人机充电器
public boolean drone_live = false; //无人机推流状态
public boolean in_live = false; //内部摄像头推流状态
public boolean out_live = false; //外部摄像头推流状态
public boolean drone_connect = false; //无人机连接状态
}
布局: