在Android中添加提醒事件失败

时间:2015-06-10 15:55:44

标签: events calendar reminders

我有一个方法可以为事件添加提醒,但它失败了:

FATAL EXCEPTION: main
android.database.sqlite.SQLiteException
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:184)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
        at android.content.ContentProviderProxy.insert(ContentProviderNative.java:420)
        at android.content.ContentResolver.insert(ContentResolver.java:864)
        at de.appwege.droid.medwege.navigationdrawer.TerminFragment.insertReminder(TerminFragment.java:848)

有问题的方法:

public long insertReminder(long eventID, int minutes){
    ContentResolver cr = getActivity().getContentResolver();
    ContentValues values = new ContentValues();
    values.put(CalendarContract.Reminders.MINUTES, minutes);
    values.put(CalendarContract.Reminders.EVENT_ID, eventID);
    values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
    Uri uri = cr.insert(CalendarContract.Reminders.CONTENT_URI, values);
    return Long.valueOf(uri.getLastPathSegment());
}

我在这里缺少什么? eventID和分钟都定义了......

1 个答案:

答案 0 :(得分:0)

最近,我也遇到了同样的问题。终于找到解决办法了。

首先,您必须从设备中找到所有登录的 gmail id,然后选择任何一个 gmail 帐户并找到其日历 id。之后,您必须将该 id 传递给这样的事件查询....

<块引用>

values.put(Events.CALENDAR_ID, calendarId);

最后调用你的函数

public long insertReminder(long eventID, int minutes){
      ContentResolver cr = getActivity().getContentResolver();
      ContentValues values = new ContentValues();
      values.put(CalendarContract.Reminders.MINUTES, minutes);
      values.put(CalendarContract.Reminders.EVENT_ID, eventID);
      values.put(CalendarContract.Reminders.METHOD, 
      
      CalendarContract.Reminders.METHOD_ALERT);
      Uri uri = cr.insert(CalendarContract.Reminders.CONTENT_URI, values);
      return Long.valueOf(uri.getLastPathSegment());
}

请参阅以下查找电子邮件 ID 的方法...

 public static Hashtable listCalendarId(Context context) {

    try {
        if (haveCalendarReadWritePermissions((Activity) context)) {

            String projection[] = {"_id", "calendar_displayName"};
            Uri calendars;
            calendars = Uri.parse("content://com.android.calendar/calendars");

            ContentResolver contentResolver = c.getContentResolver();
            Cursor managedCursor = contentResolver.query(calendars, projection, null, null, null);

            if (managedCursor.moveToFirst()) {
                String calName;
                String calID;
                int cont = 0;
                int nameCol = managedCursor.getColumnIndex(projection[1]);
                int idCol = managedCursor.getColumnIndex(projection[0]);
                Hashtable<String, String> calendarIdTable = new Hashtable<>();
                do {
                    calName = managedCursor.getString(nameCol);
                    calID = managedCursor.getString(idCol);
                    Log.v(TAG, "CalendarName:" + calName + " ,id:" + calID);
                    calendarIdTable.put(calName, calID);
                    cont++;
                } while (managedCursor.moveToNext());

                managedCursor.close();

                return calendarIdTable;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}