首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Android 日曆操作實戰:增刪改查 Calendar Provider 完整代碼

     編輯:Java 相關知識

概述

Android 通過 CalendarContract ContentProvider 提供日曆數據訪問。本文提供增刪改查核心代碼,適用於需要讀寫系統日曆的 App(需 READ_CALENDAR/WRITE_CALENDAR 權限)。

核心操作代碼

1. 讀取日曆事件

public static void getCalendarInfo(Activity activity, String tag) {
    String[] projection = {
        CalendarContract.Events._ID,
        CalendarContract.Events.TITLE,
        CalendarContract.Events.DTSTART,
        CalendarContract.Events.DTEND
    };
    ContentResolver cr = activity.getContentResolver();
    try (Cursor cursor = cr.query(
            CalendarContract.Events.CONTENT_URI,
            projection, null, null, null)) {

        if (cursor != null) {
            Log.d(tag, "事件數: " + cursor.getCount());
            while (cursor.moveToNext()) {
                long id = cursor.getLong(0);
                String title = cursor.getString(1);
                long start = cursor.getLong(2);
                long end = cursor.getLong(3);
                Log.d(tag, id + ": " + title + " [" + new Date(start) + " - " + new Date(end) + "]");
            }
        }
    }
}

2. 新增事件 (跳轉系統日曆頁面)

public static void addCalendarEvent(Activity activity, String tag) {
    Intent intent = new Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI);
    intent.putExtra(CalendarContract.Events.TITLE, "產品發布會");
    intent.putExtra(CalendarContract.Events.DESCRIPTION, "年度新品發布");
    intent.putExtra(CalendarContract.Events.EVENT_LOCATION, "北京國際會議中心");
    intent.putExtra(CalendarContract.Events.ALL_DAY, false);

    Calendar cal = Calendar.getInstance();
    cal.set(2024, Calendar.JUNE, 15, 14, 0); // 2024-06-15 14:00
    intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, cal.getTimeInMillis());
    cal.add(Calendar.HOUR, 2);
    intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, cal.getTimeInMillis());

    activity.startActivity(intent); // 用戶確認後保存
}

3. 編輯指定事件

public static void editCalendarEvent(Activity activity, long eventId) {
    Uri uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventId);
    Intent intent = new Intent(Intent.ACTION_EDIT, uri);
    intent.putExtra(CalendarContract.Events.TITLE, "更新後的標題");
    // 可修改時間、地點等...
    activity.startActivity(intent);
}

4. 查看指定時間日曆

public static void viewCalendar(Activity activity, long timeInMillis) {
    Uri uri = Uri.parse("content://com.android.calendar/time/" + timeInMillis);
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    activity.startActivity(intent);
}

權限聲明 (AndroidManifest.xml)

<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

Android 6.0+ 需運行時動態申請權限。

進階:直接寫入數據庫 (繞過系統 UI)

ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startTime);
values.put(CalendarContract.Events.DTEND, endTime);
values.put(CalendarContract.Events.TITLE, "標題");
values.put(CalendarContract.Events.DESCRIPTION, "描述");
values.put(CalendarContract.Events.CALENDAR_ID, 1); // 必須指定日曆 ID
values.put(CalendarContract.Events.EVENT_TIMEZONE, "Asia/Shanghai");
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
long eventId = Long.parseLong(uri.getLastPathSegment());

注意:直接寫入需處理同步、提醒、參會人等關聯表,複雜度高,推薦使用 Intent 方式讓系統 App 完成。

總結

善用 CalendarContract 與 Intent,輕鬆實現日曆集成。核心權限、時間毫秒值、Uri 構建是關鍵點。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved