代码之家  ›  专栏  ›  技术社区  ›  WISHY

日历事件不显示?

  •  0
  • WISHY  · 技术社区  · 6 年前

    ContentResolver cr = getCurrentContext().getContentResolver();
                    ContentValues values = new ContentValues();
    
                    Calendar startDate = Calendar.getInstance();
                    startDate.setTimeInMillis(1538677837930L);
                    Calendar endDate = Calendar.getInstance();
                    endDate.setTimeInMillis(1538674237930L);
    
                    values.put(CalendarContract.Events.DTSTART, startDate.getTimeInMillis());
                    values.put(CalendarContract.Events.DTEND, endDate.getTimeInMillis());
                    values.put(CalendarContract.Events.TITLE, "event test");
                    values.put(CalendarContract.Events.DESCRIPTION, "event desc");
                    values.put(CalendarContract.Events.CALENDAR_ID, 3);
                    values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
    
                    Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
                    long eventID = Long.parseLong(uri.getLastPathSegment());
                    Log.e("event", "" + eventID);
                    setCalendarEventId(eventID);
                    Toast.makeText(getCurrentContext(), contentDatum.getGist().getTitle() + " added to your calendar.", Toast.LENGTH_SHORT).show();
    

    不过,我确实收到了日历事件id。但该事件不会显示在日历应用程序中。

    我正在三星7操作系统上运行我的代码。有什么解决办法吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   WISHY    6 年前

    结果表明,默认日历ID可能因设备而异。 私有int getCalendarId(){

        Cursor cursor = null;
        ContentResolver contentResolver = getCurrentActiveContext().getContentResolver();
        Uri calendars = CalendarContract.Calendars.CONTENT_URI;
    
        String[] EVENT_PROJECTION = new String[]{
                CalendarContract.Calendars._ID,                           // 0
                CalendarContract.Calendars.ACCOUNT_NAME,                  // 1
                CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,         // 2
                CalendarContract.Calendars.OWNER_ACCOUNT,                 // 3
                CalendarContract.Calendars.IS_PRIMARY                     // 4
        };
    
        int PROJECTION_ID_INDEX = 0;
        int PROJECTION_ACCOUNT_NAME_INDEX = 1;
        int PROJECTION_DISPLAY_NAME_INDEX = 2;
        int PROJECTION_OWNER_ACCOUNT_INDEX = 3;
        int PROJECTION_VISIBLE = 4;
    
        cursor = contentResolver.query(calendars, EVENT_PROJECTION, null, null, null);
    
        if (cursor.moveToFirst()) {
            String calName;
            long calId = 0;
            String visible;
            do {
                calName = cursor.getString(PROJECTION_DISPLAY_NAME_INDEX);
                calId = cursor.getLong(PROJECTION_ID_INDEX);
                visible = cursor.getString(PROJECTION_VISIBLE);
                Log.e("Calendar Id : ", "" + calId + " : " + calName + " : " + visible);
                if (visible.equals("1")) {
                    return (int) calId;
                }
    
            } while (cursor.moveToNext());
    
            return (int) calId;
        }
        return 1;
    
    
    }
    

    下面的代码只是为了了解有多少日历及其id的信息

            String projection[] = {"_id", "calendar_displayName"};
        Uri calendars;
        calendars = Uri.parse("content://com.android.calendar/calendars");
    
        ContentResolver contentResolver = getCurrentContext().getContentResolver();
        Cursor managedCursor = contentResolver.query(calendars, projection, null, null, null);
        String calID="1";
        if (managedCursor.moveToFirst()){
            String calName;
    
            int cont= 0;
            int nameCol = managedCursor.getColumnIndex(projection[1]);
            int idCol = managedCursor.getColumnIndex(projection[0]);
            do {
                calName = managedCursor.getString(nameCol);
                calID = managedCursor.getString(idCol);
                Log.e("calName",""+calName);
                Log.e("calID",""+calID);
                cont++;
            } while(managedCursor.moveToNext());
            managedCursor.close();
        }