/** * 计时器开始 * */ private static final String TAG="AccountRegisterActivity"; private void startTimeView(){ String codeTime=spUtils.getLoginCodeTime(); if (TextUtils.isEmpty(codeTime)){ // 如果当前没有记录,就先保存下当前毫米数,然后开始 spUtils.saveLoginCodeTime(GetCodePhone.getText().toString()+","+System.currentTimeMillis()/1000); handler_Msg.sendEmptyMessage(CURRENT_TIME_VIEW_TYPE); return; } // 区别一下不同手机号的计时 String phone=codeTime.substring(0,codeTime.indexOf(",")); String startTime=codeTime.substring(codeTime.indexOf(",")+1,codeTime.length()); String phone_in=GetCodePhone.getText().toString(); Log.e(TAG, "startTimeView phone: "+phone ); Log.e(TAG, "startTimeView startTime: "+startTime ); Log.e(TAG, "startTimeView phone_in: "+phone_in ); if (phone.equals(phone_in)){ // 存储的手机号和当前手机号一样,并且时间相差在60秒以内; if (60>(System.currentTimeMillis()/1000-Long.parseLong(startTime))){ long time=System.currentTimeMillis()/1000-Long.parseLong(startTime); Log.e(TAG, "startTimeView time: "+time ); Message msg=new Message(); msg.what=CURRENT_TIME_VIEW_TYPE; msg.obj=time; handler_Msg.sendEmptyMessage(CURRENT_TIME_VIEW_TYPE); return; } } spUtils.saveLoginCodeTime(GetCodePhone.getText().toString()+","+System.currentTimeMillis()/1000); handler_Msg.sendEmptyMessage(CURRENT_TIME_VIEW_TYPE); // 如果存下来的时间和当前时间相差在60秒以内,就继续计时 } private static final int CURRENT_TIME_VIEW_TYPE=1000; private Handler handler_Msg=new Handler(){ @Override public void handleMessage(@NonNull Message msg) { switch (msg.what){ case CURRENT_TIME_VIEW_TYPE: long time=60; if (msg.obj!=null){ time= (Long) msg.obj; } tv_code_get.setText(time+""); if (time!=0){ time--; } Message msg_xun=new Message(); msg_xun.obj=time; msg_xun.what=CURRENT_TIME_VIEW_TYPE; handler_Msg.sendMessageDelayed(msg_xun,1000); break; } } };