android - 将事件添加到 native 日历不起作用

标签 android android-contentprovider android-calendar

我正在开发 Android 应用程序。我必须将事件添加到 native Android 日历。所以我尝试了以下代码:

if (Build.VERSION.SDK_INT >= 8 ) {
    l_eventUri = Uri.parse("content://com.android.calendar/events");
} else {
    l_eventUri = Uri.parse("content://calendar/events");
}

Cursor cursor = getContentResolver() .query(Uri.parse(getCalendarUriBase(this)),new String[] { "calendar_id", "displayname"}, null,null, null);
cursor.moveToFirst();
// fetching calendars name
String CNames[] = new String[cursor.getCount()];
// fetching calendars id
int[] CalIds = new int[cursor.getCount()];
for (int i = 0; i < CNames.length; i++) {
    CalIds[i] = cursor.getInt(0);
    CNames[i] = cursor.getString(1);
    cursor.moveToNext();
}

// get calendar
Calendar cal = Calendar.getInstance();     
Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events");
ContentResolver cr = getContentResolver();

// event insert
ContentValues values = new ContentValues();
values.put("calendar_id",111);
values.put("title", "Reminder Title");
values.put("allDay", 0);
values.put("dtstart", cal.getTimeInMillis() + 11*60*1000); // event starts at 11 minutes from now
values.put("dtend", cal.getTimeInMillis()+60*60*1000); // ends 60 minutes from now
values.put("description", "Reminder description");
//Event Id
values.put("_id", 23);  
//0~ default; 1~ confidential; 2~ private; 3~ public
values.put("visibility", 0);
//0~ false; 1~ true
values.put("hasAlarm", 1);
//status: 0~ tentative; 1~ confirmed; 2~ canceled
values.put("eventStatus", 1);
//0~ opaque, no timing conflict is allowed; 1~ transparency, allow overlap of scheduling
values.put("transparency", 0);
Uri event = cr.insert(EVENTS_URI, values);

// reminder insert
Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders");
values = new ContentValues();
values.put( "event_id", Long.parseLong(event.getLastPathSegment()));
values.put( "method", 1 );
values.put( "minutes", 10 );
cr.insert( REMINDERS_URI, values );

private String getCalendarUriBase(Activity act) {
    String calendarUriBase = null;
    Uri calendars = Uri.parse("content://calendar/calendars");
    Cursor managedCursor = null;
    try {
        managedCursor = act.managedQuery(calendars, null, null, null, null);
    } catch (Exception e) {
    }

    if (managedCursor != null) {
        calendarUriBase = "content://calendar/";
    } else {
        calendars = Uri.parse("content://com.android.calendar/calendars");
        try {
            managedCursor = act.managedQuery(calendars, null, null, null, null);
        } catch (Exception e) {
        }
        if (managedCursor != null) {
            calendarUriBase = "content://com.android.calendar/";
        }
    }
    return calendarUriBase;
}

当我运行这段代码时,我得到了 IllegalArgumentException

java.lang.IllegalArgumentException: Unknown URL content://com.android.calendar/

我认为错误是因为 getCalendarUriBase() 方法。我搜索了很多其他方法,但到目前为止,大多数开发人员都遵循上述代码 fragment ,因为这适用于每个 Android 版本。如何解决?

最佳答案

当您尝试执行此行时,您错过了 calendars

Cursor cursor = getContentResolver().query(
    Uri.parse(getCalendarUriBase(this)), /* <-- unknown URL content */
    new String[] { "calendar_id", "displayname" },
    null,null,null);

导致 IllegalArgumentException

只需在 getCalendarUriBase(this) 之后添加 calendars

Cursor cursor = getContentResolver().query(
    Uri.parse(getCalendarUriBase(this)+"calendars"),
    new String[] { "_id", "displayName" },
    null,null,null);

Note:

  • Use _id to get the calendar ID if you query from calendars table. calendar_id is defined as a foreign key in other tables (e.g. events).
  • Use displayName for GingerBread or below. For Ice Cream Sandwich and above, use calendar_displayName.

添加事件时,您需要包括:日历 ID、开始日期、结束日期(或重复规则)和事件时区。除了时区之外,您已经包括了所有内容。尝试添加

values.put("eventTimezone", TimeZone.getDefault().getID());

在插入事件之前。

关于android - 将事件添加到 native 日历不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20113448/

相关文章:

android - 即使蓝牙已关闭,也能检测附近的信标

java - 问题 当我使用 @Named 注释时,如果没有 @Provides 注释的方法,则无法提供 Dagger

java - 当我尝试写入或更新 SQLite 数据库时,我的联系人应用程序停止

java - Android 日历事件问题

android - 在 Android SDK 上导入 .ics 文件并添加到日历

java - 防止键盘隐藏 View android

android - Jetpack Compose 中的副作用

android - 通过 Android 上的 ContentProvider 在应用程序的数据目录中共享图像

java - 需要帮助了解 Android 中处理异常的位置

java - Material 日历 View 高度环绕问题