安卓(Android) 怎么保存日历事件(Calendar Event)

2019年10月2日21:51:43安卓(Android) 怎么保存日历事件(Calendar Event)已关闭评论 353 1529字阅读5分5秒

1.简介

安卓(Android) 怎么保存日历事件(Calendar Event)?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.CalendarContract;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class Main{
    public static boolean saveCalendarEvent(Context context,
            CalendarEvent calendarEvent) {
        boolean operation = false;

        if (calendarEvent != null) {

            Uri event = context.getContentResolver()
                    .insert(getEventsContentUri(),
                            calendarEvent.getContentValues());

            if (event != null) {

                
                long eventID = Long.parseLong(event.getLastPathSegment());

                for (CalendarReminder reminder : calendarEvent.reminders) {
                    context.getContentResolver().insert(
                            getRemindersContentUri(),
                            reminder.getContentValues());
                }
                operation = true;
            }
        }

        return operation;
    }
    @SuppressLint("NewApi")
    private static Uri getEventsContentUri() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            return CalendarContract.Events.CONTENT_URI;
        } else {
            return Uri.parse(getContentUri() + "/events");
        }
    }
    @SuppressLint("NewApi")
    private static Uri getRemindersContentUri() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            return CalendarContract.Reminders.CONTENT_URI;
        } else {
            return Uri.parse(getContentUri() + "/reminders");
        }
    }
    @SuppressLint("NewApi")
    private static String getContentUri() {
        String AUTHORITY = "";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
            AUTHORITY = "com.android.calendar";
        } else {
            AUTHORITY = "calendar";
        }
        return "content:
    }
}

 
继续阅读
编程十万个怎么办