class ReportLocationHandler extends Handler {\n\n private static final int REPORT_LOCATION = 1;\n private static final int UPDATE_TIME = 2;\n private static final long UPDATE_INTERVAL = 1000; // 更新时间间隔\n\n @Override\n public void handleMessage(Message msg) {\n switch (msg.what) {\n case REPORT_LOCATION:\n onReportLocation();\n break;\n case UPDATE_TIME:\n updateTime();\n break;\n }\n }\n\n private void updateTime() {\n SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");\n String time = simpleDateFormat.format(new Date());\n String[] timeArray = time.split(" ");\n\n binding.tvData.setText(timeArray[0]);\n binding.tvTime.setText(timeArray[1]);\n\n if (!MainActivity.this.isDestroyed()) {\n sendEmptyMessageDelayed(UPDATE_TIME, UPDATE_INTERVAL);\n }\n }\n\n public void startUpdatingTime() {\n sendEmptyMessage(UPDATE_TIME);\n }\n\n public void stopUpdatingTime() {\n removeMessages(UPDATE_TIME);\n }\n}

Android 时钟倒计时优化:解决跳秒问题

原文地址: https://www.cveoy.top/t/topic/pwKW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录