Android无法使用内容解析器插入新的日历事件

时间:2016-01-01 19:04:00

标签: android calendar android-contentresolver

我尝试使用日历提供程序添加日历事件。

问题是插入的事件未显示在设备的日历中。

我尝试使用URI和内容解析器解决它,但使用内容解析器会抛出异常。

Only the provider may write to sync_events

如何以正确的方式将新事件插入主日历?

非常感谢任何建议。     public void pushEventtoCalendar(Activity curActivity,Context ctx){             试试{

            // Get all calendars
            String[] projection = new String[]{
                    CalendarContract.Calendars._ID,
                    CalendarContract.Calendars.NAME,
                    CalendarContract.Calendars.ACCOUNT_NAME,
                    CalendarContract.Calendars.CALENDAR_DISPLAY_NAME
            };

            Cursor cursor = ctx.getContentResolver().query(
                    CalendarContract.Calendars.CONTENT_URI,
                    projection,
                    null,
                    null,
                    CalendarContract.Calendars._ID + " ASC");

            if (cursor.moveToFirst()) {
                do {
                    long id = cursor.getLong(0);
                    String CALENDAR_DISPLAY_NAME = cursor.getString(3);
                    Logger.d(String.valueOf(id));
                    Logger.d(CALENDAR_DISPLAY_NAME);
                } while (cursor.moveToNext());
            }
            // Create and save event
            long calID = 1;
            long start = 0;
            org.joda.time.format.DateTimeFormatter dtf = org.joda.time.format.DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
            org.joda.time.LocalDateTime ldt = dtf.parseLocalDateTime("2016-01-01 07:23:34.567");
            start = ldt.toDateTime().getMillis();
            Logger.d(String.valueOf(start));

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
            format.setTimeZone(TimeZone.getTimeZone("UTC"));

            Date date = format.parse("2016-01-01 07:23:34.567");
            start = date.getTime();
            Logger.d(String.valueOf(start));

            ContentResolver cr = ctx.getContentResolver();
            ContentValues values = new ContentValues();
            ContentValues contentValues = new ContentValues();
            contentValues.put(CalendarContract.Events.CALENDAR_ID, calID);
            contentValues.put(CalendarContract.Events.DTSTART, start);
            contentValues.put(CalendarContract.Events.DTEND, start + (60 * 60 * 1000 * 1));
            contentValues.put(CalendarContract.Events.VISIBLE, 1);
            //contentValues.put(CalendarContract.Events.DURATION, "1w");
            contentValues.put(CalendarContract.Events.SYNC_EVENTS, 1);
            contentValues.put(CalendarContract.Events.ORIGINAL_ALL_DAY, true);
            contentValues.put(CalendarContract.Events.ALL_DAY, true);
            contentValues.put(CalendarContract.Events.TITLE, "Wedding Anniversary");
            contentValues.put(CalendarContract.Events.DESCRIPTION, "25 years of Happy Marriage!");
            contentValues.put(CalendarContract.Events.EVENT_TIMEZONE, "Asia/Singapore");

            if (ActivityCompat.checkSelfPermission(curActivity.getApplicationContext(), Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                Logger.e("App has not permission");
                return;
            }

            //cr.update(ContentUris.withAppendedId(CalendarContract.Calendars.CONTENT_URI, 1), values, null, null);
            Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, contentValues);
            //Uri uri = curActivity.getApplicationContext().getContentResolver().insert(CalendarContract.Events.CONTENT_URI, contentValues);
            long eventId = new Long(uri.getLastPathSegment());
            Logger.d(String.valueOf(eventId));
        } catch (Exception e) {
            Logger.e(e.getMessage());
        }
    }

0 个答案:

没有答案