如何将多个事件添加到android marshmallow日历?

时间:2016-05-21 10:16:28

标签: android

事件正在适当地添加到日历直到api 22.

我还为Marshmallow实现了运行时权限,在我的应用程序的Phone设置中允许Calender权限清晰可见。

但仍然没有更新电话日历,也没有任何错误或警告。

以下是我在电话日历上以编程方式添加事件的方法。

    private void addEventToCalender(Activity ourActivity, String title, String desc, String place, int status, long startDate, long endDte, boolean needReminder, boolean needMailService) {
        try {
            String eventUriString = "content://com.android.calendar/events";
            ContentValues eventValues = new ContentValues();
            eventValues.put("calendar_id", 1); // id, We need to choose from         // our mobile for primary its 1
            eventValues.put("title", "My Title");
            eventValues.put("description","My Description" );
            eventValues.put("eventLocation", "Noida,UP ";

            eventValues.put("dtstart", startDate);
            eventValues.put("dtend", endDte);
            eventValues.put("allDay", 1); // 1 for whole day 
            //eventValues.put("rrule", "FREQ=YEARLY");

            // values.put("allDay", 1); //If it is bithday alarm or such
            // kind (which should remind me for whole day) 0 for false, 1
            // for true
            eventValues.put("eventStatus", 1); // This information is
            // sufficient for most
            // entries tentative (0),
            // confirmed (1) or canceled
            // (2):
            eventValues.put("eventTimezone", "UTC/GMT " + Constants.tzone);
            eventValues.put("hasAlarm", 1); // 0 for false, 1 for true
            Uri eventUri = this.getApplicationContext().getContentResolver().insert(Uri.parse(eventUriString), eventValues);
            long eventID = Long.parseLong(eventUri.getLastPathSegment());
            Log.i("eventID", eventID + "");
            showSnackBar("Event added to calender successfuly.");
        } catch (Exception ex) {
            Log.e("error", "Error in adding event on calendar" + ex.getMessage());
            showSnackBar("Ünable to add event to calender!");
        }

    }

1 个答案:

答案 0 :(得分:0)

可能你想在Fragment里面使用。

初次使用时:

super.requestPermissions( new String[]{Manifest.permission.WRITE_CALENDAR}, MY_PERMISSIONS_REQUEST_WRITE_CALENDAR);

内部片段,您需要调用:

FragmentCompat.requestPermissions(permissionsList, RequestCode)

注意:

ActivityCompat.requestPermissions(Activity, permissionsList, RequestCode);

在app.gradle中为FragmentCompat类添加此库。

compile 'com.android.support:support-v13:version_of_library'