Giới thiệu
Bài viết này hướng dẫn cách thêm sự kiện vào ứng dụng Lịch (Calendar) mặc định của Android từ ứng dụng của bạn bằng Kotlin. Chúng ta sẽ sử dụng Intent.ACTION_EDIT để mở màn hình tạo sự kiện với các thông tin được điền sẵn.
Các bước thực hiện
Bước 1: Tạo dự án mới
Trong Android Studio, chọn File → New Project và cấu hình dự án.
Bước 2: Thiết kế giao diện (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerInParent="true"
android:onClick="addCalendarEvent"
android:text="Add Calendar Event" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Calendar Event In Android"
android:textColor="@android:color/background_dark"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
Bước 3: Xử lý logic (src/MainActivity.kt)
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import java.util.Calendar
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
}
fun addCalendarEvent(view: View) {
val calendarEvent = Calendar.getInstance()
val intent = Intent(Intent.ACTION_EDIT)
intent.type = "vnd.android.cursor.item/event"
intent.putExtra("beginTime", calendarEvent.timeInMillis)
intent.putExtra("allDay", true)
intent.putExtra("rule", "FREQ=YEARLY")
intent.putExtra("endTime", calendarEvent.timeInMillis + 60 * 60 * 1000) // 1 giờ sau
intent.putExtra("title", "Calendar Event")
startActivity(intent)
}
}
Bước 4: Cấu hình Manifest (androidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.q11">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Chạy ứng dụng
Kết nối thiết bị thật, nhấn Run trong Android Studio. Khi nhấn nút "Add Calendar Event", ứng dụng Lịch sẽ mở với form tạo sự kiện đã điền sẵn tiêu đề, thời gian bắt đầu/kết thúc và quy tắc lặp lại hàng năm.
Các trường Intent quan trọng
beginTime: Thời gian bắt đầu (milliseconds từ epoch)endTime: Thời gian kết thúctitle: Tiêu đề sự kiệnallDay: true nếu là sự kiện cả ngàyrule: Quy tắc lặp lại (RRULE format), ví dụ "FREQ=YEARLY"description: Mô tả chi tiết (tùy chọn)eventLocation: Địa điểm (tùy chọn)
Lưu ý
Intent này yêu cầu thiết bị có ứng dụng Lịch mặc định. Nếu không có, hệ thống sẽ hiển thị hộp thoại cho phép người dùng chọn ứng dụng xử lý.