Phát triển Android: Đồng bộ hóa thời gian hệ thống

Đồng bộ hóa thời gian trong ứng dụng Android

Vấn đề đặt ra là làm thế nào để đồng bộ thời gian hiển thị trong ứng dụng với thời gian hệ thống của thiết bị. Bài viết này trình bày ví dụ minh họa cách thiết lập thời gian sử dụng TimePickerDialog và cập nhật giao diện theo thời gian hệ thống.

Triển khai phần xử lý thời gian

Tệp MainTimeActivity.java chứa logic xử lý chọn thời gian:

package com.example.time_sync_demo;

import java.util.Calendar;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainTimeActivity extends Activity {
    private TextView timeDisplay;
    private Button timePickerBtn;
    private int currentHour, currentMinute;

    private static final int TIME_PICKER_DIALOG = 0;

    private TimePickerDialog.OnTimeSetListener timeChangeListener = 
        new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hour, int minute) {
                currentHour = hour;
                currentMinute = minute;
                refreshTimeDisplay();
            }
        };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.time_layout);

        timeDisplay = findViewById(R.id.time_text);
        timePickerBtn = findViewById(R.id.time_button);

        timePickerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(TIME_PICKER_DIALOG);
            }
        });

        Calendar systemTime = Calendar.getInstance();
        currentHour = systemTime.get(Calendar.HOUR_OF_DAY);
        currentMinute = systemTime.get(Calendar.MINUTE);
        refreshTimeDisplay();
    }

    private void refreshTimeDisplay() {
        timeDisplay.setText(String.format("%02d:%02d", currentHour, currentMinute));
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        if (id == TIME_PICKER_DIALOG) {
            return new TimePickerDialog(this, timeChangeListener, currentHour, currentMinute, true);
        }
        return null;
    }
}

Giao diện người dùng

Tệp time_layout.xml định nghĩa giao diện:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:id="@+id/time_text"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:textSize="24sp"
        android:layout_marginBottom="16dp"/>

    <Button
        android:id="@+id/time_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Chọn thời gian"/>

</LinearLayout>

Giải pháp đồng bộ hóa

Để đồng bộ với hệ thống, ứng dụng cần:

  • Khởi tạo giá trị ban đầu từ Calendar.getInstance()
  • Không thiết lập thời gian hệ thống (Android không cho phép ứng dụng thường thay đổi thời gian hệ thống)
  • Sử dụng BroadcastReceiver để lắng nghe sự kiện TIME_CHANGED khi người dùng thay đổi thời gian

Thẻ: Android TimePickerDialog Calendar TextView Button

Đăng vào ngày 13 tháng 7 lúc 02:55