【问题标题】:Added events in google calendar not display in Android Pie?谷歌日历中添加的事件不显示在 Android Pie 中?
【发布时间】:2019-05-28 19:59:58
【问题描述】:

我想以编程方式在谷歌日历中添加事件,并且我有以下代码可以在我的日历中添加事件:

try {
            ContentResolver cr = getContentResolver();
            ContentValues values = new ContentValues();

            String calendarId = getGmailCalendarId(mContext);

            values.put(CalendarContract.Events.DTSTART, startMillis);
            values.put(CalendarContract.Events.DTEND, endMillis);
            values.put(CalendarContract.Events.TITLE, title);
            //values.put(CalendarContract.Events.DESCRIPTION, description);
            values.put(CalendarContract.Events.EVENT_LOCATION, location);
            values.put(CalendarContract.Events.HAS_ALARM, false);
            values.put(CalendarContract.Events.CALENDAR_ID, calendarId);
            values.put(CalendarContract.Events.EVENT_TIMEZONE, Calendar.getInstance()
                    .getTimeZone().getID());

            System.out.println(Calendar.getInstance().getTimeZone().getID());

//            Uri uri;
//            if (Integer.parseInt(Build.VERSION.SDK) >= 8)
//                uri = cr.insert(Uri.parse("content://com.android.calendar/events"), values);
//            else
//                uri = cr.insert(Uri.parse("content://calendar/events"), values);

            Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
            long eventId = java.lang.Long.parseLong(((Uri) uri).getLastPathSegment());
            Log.d("Event_Id", eventId + "");
            syncCalendar(mContext, calendarId);

        } catch (Exception e) {
            e.printStackTrace();
        }

我使用此方法获得了正确的 gmail id,该方法返回了我的 calendarId。

getGmailCalendarId()

public String getGmailCalendarId(Context c) {
        String calenderId = "";
        String[] projection = new String[]{"_id", "calendar_displayName"};
        Uri calendars = Uri.parse("content://com.android.calendar/calendars");
        ContentResolver contentResolver = c.getContentResolver();
        Cursor managedCursor = contentResolver.query(calendars,
                projection, null, null, null);

        if (managedCursor != null && managedCursor.moveToFirst()) {
            String calName;
            String calID;
            int nameCol = managedCursor.getColumnIndex(projection[1]);
            int idCol = managedCursor.getColumnIndex(projection[0]);
            do {
                calName = managedCursor.getString(nameCol);
                calID = managedCursor.getString(idCol);
                if (calName.contains("@gmail")) {
                    calenderId = calID;
                    break;
                }


            } while (managedCursor.moveToNext());
            managedCursor.close();
            return calenderId;
        }

        return calenderId;

    }

同步日历

public static void syncCalendar(Context context, String calendarId) {
    try {
        ContentResolver cr = context.getContentResolver();
        ContentValues values = new ContentValues();
        values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
        values.put(CalendarContract.Calendars.VISIBLE, 1);

        Uri updateUri = ContentUris.withAppendedId(CalendarContract.Calendars.CONTENT_URI, Long.parseLong(calendarId));
        cr.update(updateUri, values, null, null);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

一切都很完美,没有任何错误。它返回我的事件 id,这意味着事件添加成功。 问题是,除了 android pie (9) 之外,所有设备的 google 日历中都可以看到添加的事件。在 android pie 中,它返回我的事件 ID,但事件未显示在日历应用程序中。

添加的事件在所有设备的谷歌日历中可见,除了 安卓派(9)

我关注了许多 SO 问题和答案,但对日历的 android pie 问题一无所知。

【问题讨论】:

  • 当我使用您的代码添加事件时,它会延迟显示在谷歌日历中。比如2-3分钟。你有类似的问题吗?

标签: android google-calendar-api


【解决方案1】:

我为这个问题奋斗了很多天,但最终我解决了它。问题在于我的 android pie 设备中的 google 帐户。

我遵循的步骤是

  • 从设备上注销所有 google 帐户
  • 然后使用所有帐户登录
  • 运行应用并执行代码。

之后它在我的设备上运行良好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    相关资源
    最近更新 更多