前提:收送指令皆是透过socket,手机为client端,另一设备为service端,service端会根据收到的指令,回复不同的讯息。只需负责手机端的行为。
问题:当user按下手中画面中的listview的选项时,系统先送出A指令,收到service回复的ack后再送B指令。
送出B指令之后等待 (35 + n*1) 秒 ( n为一个常数),等待时间到达后,送出C指令。
送出C指令后等待10秒,再收service回复的信息,这时可能会是收到:RsrR@B=1=050505=1000000000=2105=ZN,则表示service端还没把需要的信息搜集好。
手机会再送一次C指令后再等10秒,再收service回复的信息,如果还是收到一样的讯息,则又再重复上述的行为一次(C指令总共只会送三次)。
如果在这重复三次的行为中有收到这种格式:RsrR@B=1=050505=1000000000=2105=02=030303=02020A,非ZN结尾的,则表示service端已把需要的信息搜集好,则停止送C指令的行为,直接跳转至另一个页面。
若三次都没有收到RsrR@B=1=050505=1000000000=2105=02=030303=02020A,这种格式的话,service端还是会送出另一种讯息。
想请问上述这些时间延迟的行为,要怎么写,我目前是写成这样,但执行出来的结果与想达成的目的不符合,求解。
private AdapterView.OnItemClickListener mOnItemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
//系统先送出A指令
handler.postDelayed(new Runnable() {
@Override
public void run() {
final String PWD = SharePreferenceUtil.get_HG_PWD(ActivityFirstLinkMPR.this);
final String SSID = SharePreferenceUtil.get_HG_SSID(ActivityFirstLinkMPR.this);
String hgcmd;
hgcmd = CmdCtrl.HGMakeCmd(
CmdCtrl.CMD_SYS_HG,
CmdCtrl.CMD_DST_ALL, null,
PWD,
SSID);
System.out.println("Assign HG MakeCmd:" + hgcmd);
/* send cmd */
startService(
new Intent(ActivityFirstLinkMPR.this, NotificationService.class)
.setAction(NotificationService.ACTION_SENT)
.putExtra(NotificationService.EXTRA_PARAM_TARGET_DATA, hgcmd));
//收到service回复的ack
handler.postDelayed(new Runnable() {
@Override
public void run() {
startService(new Intent(ActivityFirstLinkMPR.this, NotificationService.class)
.setAction(NotificationService.ACTION_RECV_MPRMAC));
if (SharePreferenceUtil.get_MPR_MAC(ActivityFirstLinkMPR.this) != null) {
Toast.makeText(ActivityFirstLinkMPR.this, "You have received MPR MAC.", Toast.LENGTH_SHORT).show();
final String mprmac = SharePreferenceUtil.get_MPR_MAC(ActivityFirstLinkMPR.this);
final String lightnum = SharePreferenceUtil.get_HG_LIGHT_NUMBER(ActivityFirstLinkMPR.this);
String setmprcmd;
setmprcmd = CmdCtrl.SetMPRMakeCmd(
CmdCtrl.CMD_MOD_SET,
mprmac,
"168",
"1",
lightnum);
System.out.println("Set MakeCmd:" + setmprcmd);
/* send cmd */
startService(
new Intent(ActivityFirstLinkMPR.this, NotificationService.class)
.setAction(NotificationService.ACTION_SENT)
.putExtra(NotificationService.EXTRA_PARAM_TARGET_DATA, setmprcmd));
int numbersec = Integer.parseInt(lightnum);
final int sec = 35 + (numbersec / 2) * 1000;
//送B指令
handler.postDelayed(new Runnable() {
@Override
public void run() {
String membercmd;
membercmd = CmdCtrl.MmmemberMakeCmd(
CmdCtrl.CMD_SYS_REQ,
mprmac,
"2015=",
"ZG");
System.out.println("MemberMakeCmd:" + membercmd);
/* send cmd */
startService(
new Intent(ActivityFirstLinkMPR.this, NotificationService.class)
.setAction(NotificationService.ACTION_SENT)
.putExtra(NotificationService.EXTRA_PARAM_TARGET_DATA, membercmd));
handler.postDelayed(new Runnable() {
@Override
public void run() {
startService(new Intent(ActivityFirstLinkMPR.this, NotificationService.class)
.setAction(NotificationService.ACTION_RECV_MPRMAC));
String memberlist = SharePreferenceUtil.get_MemberList(ActivityFirstLinkMPR.this);
String listYN = memberlist.substring(memberlist.length() - 2);
if (listYN.equals("ZN")) {
System.out.println("wait member add");
Toast.makeText(ActivityFirstLinkMPR.this, "wait member add", Toast.LENGTH_SHORT).show();
handler.postDelayed(new Runnable() {
@Override
public void run() {
startService(new Intent(ActivityFirstLinkMPR.this, NotificationService.class)
.setAction(NotificationService.ACTION_RECV_MPRMAC));
String memberlist = SharePreferenceUtil.get_MemberList(ActivityFirstLinkMPR.this);
String listYN = memberlist.substring(memberlist.length() - 2);
System.out.println("R_mbmberlist -2 :" + listYN);
if (listYN.equals("ZN")) {
Toast.makeText(ActivityFirstLinkMPR.this, "wait 2 member add", Toast.LENGTH_SHORT).show();
handler.postDelayed(new Runnable() {
@Override
public void run() {
startService(new Intent(ActivityFirstLinkMPR.this, NotificationService.class)
.setAction(NotificationService.ACTION_RECV_MPRMAC));
String memberlist = SharePreferenceUtil.get_MemberList(ActivityFirstLinkMPR.this);
String listYN = memberlist.substring(memberlist.length() - 2);
System.out.println("R_mbmberlist -2 :" + listYN);
if (listYN.equals("ZN")) {
}
Toast.makeText(ActivityFirstLinkMPR.this, "已送三次了!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(ActivityFirstLinkMPR.this, ActivityLightSort.class));
ActivityFirstLinkMPR.this.finish();
}
}, 10000L);
}
Toast.makeText(ActivityFirstLinkMPR.this, "2 member add OK", Toast.LENGTH_SHORT).show();
startActivity(new Intent(ActivityFirstLinkMPR.this, ActivityLightSort.class));
ActivityFirstLinkMPR.this.finish();
}
}, 10000L);
}
Toast.makeText(ActivityFirstLinkMPR.this, "member add OK", Toast.LENGTH_SHORT).show();
startActivity(new Intent(ActivityFirstLinkMPR.this, ActivityLightSort.class));
ActivityFirstLinkMPR.this.finish();
}
}, 10000L);
}
}, sec);
} else {
Toast.makeText(ActivityFirstLinkMPR.this, "MPR MAC not received.", Toast.LENGTH_SHORT).show();
}
}
}, 500L);
}
},100L);
}
};