소 백 개발, handler 순서 질문?

전제: 수신 명령 은 모두 socket 을 통 해 핸드폰 은 client 단 이 고 다른 장 치 는 service 단 이 며 service 단 은 받 은 명령 에 따라 서로 다른 메 시 지 를 답장 합 니 다.휴대 전화 측의 행동 만 책임 져 야 한다.
문제: user 가 손 에 들 고 있 는 화면 에 있 는 listview 옵션 을 누 르 면 시스템 은 먼저 A 명령 을 보 내 고 service 가 답장 한 ack 을 받 은 후에 B 명령 을 보 냅 니 다.
B 명령 을 보 내 고 기 다 립 니 다. (35 + n*1) 초. ( n 은 상수) 시간 이 도착 하면 C 명령 을 보 냅 니 다.
C 명령 을 보 낸 후 10 초 를 기 다 렸 다가 service 가 답장 한 메 시 지 를 받 습 니 다. 이 때 받 을 수 있 습 니 다.RsrR@B= 1 = 050505 = 100000000 = 2105 = ZN 은 service 측 이 필요 한 정 보 를 아직 수집 하지 않 았 음 을 나타 낸다.
휴대 전 화 는 C 명령 을 한 번 더 보 낸 뒤 10 초 를 더 기 다 렸 다가 서비스 가 답장 한 메 시 지 를 받 고, 같은 메 시 지 를 받 았 다 면 이 같은 행 위 를 한 번 더 반복 한다 (C 명령 은 총 세 번 만 보 낼 수 있다).
만약 이 세 번 반복 되 는 행위 에서 이러한 형식 을 받 았 다 면:RsrR@B= 1 = 050505 = 100000000 = 2105 = 02 = 030303 = 020A, ZN 이 아 닌 경우 service 측 이 필요 한 정 보 를 수집 했다 는 뜻 으로 C 명령 을 보 내 는 행 위 를 중단 하고 다른 페이지 로 바로 이동 합 니 다.
세 번 도 못 받 으 면RsrR@B= 1 = 050505 = 100000000 = 2105 = 02 = 030303 = 0200 A, 이런 포맷 이면 서비스 쪽 에서 또 다른 메 시 지 를 보 냅 니 다.
상술 한 시간 지연 행 위 를 어떻게 써 야 하 는 지 여 쭤 보고 싶 습 니 다. 저 는 지금 이렇게 썼 지만 실 행 된 결 과 는 달성 하고 자 하 는 목적 과 부합 되 지 않 습 니 다.
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);
        }
    };

좋은 웹페이지 즐겨찾기