Android 开发 重写定位器类Timer与TimerTask

class AttendanceTimer extends Timer {
        private static final int LOCATION = 0x01;
        private static final int WAIT = 0x02;
        private static final int START = 0x03;
        public int mId;
        private TimerTask mTimerTask;
        private AttendanceData mAttendanceData;
        public Handler mHandler;
        private LocationBean mBean;
        private int count = 0;

        public AttendanceTimer(final int id){
            L.i("考勤-短定时器初始化中");
            this.mId = id;

            mHandler = new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    switch (msg.what){
                        case LOCATION:
                            EventBus.post(new Event(EventCode.EVENT_LOCATION_POST_ATTENDANCE));
                            L.ii(TAG,"考勤-短定时器定位请求");
                            mHandler.sendEmptyMessageDelayed(WAIT,1000);
                            break;
                        case WAIT:
                            L.i("考勤-短定时器进入等待处理");
                            if (mBean == null){
                                count++;
                                mHandler.sendEmptyMessageDelayed(WAIT,1000);
                                L.ii(TAG,"考勤-短定时器定时数据是空再次进入等待处理");
                                if (count > 5){ //5次后重新发送定位请求
                                    EventBus.post(new Event(EventCode.EVENT_LOCATION_POST_ATTENDANCE));
                                    count = 0;
                                }
                                break;
                            }
                            count = 0;
                            mHandler.sendEmptyMessage(START);
                            break;
                        case START:
                            L.ii(TAG,"考勤-短定时器启动 longitude="+mBean.longitude+" latitude="+mBean.latitude);
                            L.ii(TAG,"考勤-短定时器启动 longitude="+mAttendanceData.longitude+" latitude="+mAttendanceData.latitude+" radius="+mAttendanceData.radius);
                            if (CoordinateUtil.isInCircle(mBean.longitude,mBean.latitude,mAttendanceData.longitude,mAttendanceData.latitude,String.valueOf(mAttendanceData.radius))){
                                mAttendanceoutApi.dopost(Session.I().getToken(),String.valueOf(mId),String.valueOf(System.currentTimeMillis()));
                                mBufferId = mId;
                                mBufferTime = System.currentTimeMillis();
                                L.ii(TAG,"考勤-判断考勤触发,上报考勤 id="+mId);
                                destroy();
                                mBean = null;
                                break;
                            }
                            L.ii(TAG,"考勤-没有进入考勤位置,创建并且等待下一次判断");
                            mBean = null;
                            mTimerTask.cancel();
                            mTimerTask = null;
                            initTimerTask();
                            purge();
                            schedule(mTimerTask,30*1000);
                            break;
                        default:
                            break;

                    }
                }
            };
            initData();
            initTimerTask();
            schedule(mTimerTask,1*1000);
        }

        private void initData(){
            mAttendanceData = DataBaseSession.I().getIsIdAttenanceData(mId);

        }

        private void initTimerTask(){
            mTimerTask = new TimerTask() {
                @Override
                public void run() {
                    mHandler.sendEmptyMessage(LOCATION);
                }
            };
        }

        public void destroy(){
            cancel();
            purge();
            if (mTimerTask != null){
                mTimerTask.cancel();
                mTimerTask = null;
            }
            if(mHandler != null){
                mHandler.removeCallbacksAndMessages(null);
                mHandler = null;
            }


        }

        public void setBean(LocationBean mBean) {
            L.i("考勤-短定时器设置定位数据");
            this.mBean = mBean;
        }
    }

 

Android 开发 重写定位器类Timer与TimerTask

上一篇:SpringBoot 集成ZooKeeper 框架实现LogBack 日志输出


下一篇:android studio运行时报错AVD Nexus_5X_API_P is already running解决办法