android - 无法在android Calender中存储事件

android - 无法在android Calender中存储事件,第1张

嘿我试图从我的应用程序添加日历活动但它没有工作我无法在日历应用程序中看到我的活动我也没有得到提醒这是我的代码

public class CalendarSync {
    Context context;

    public CalendarSync(Context context) {
        this.context = context;
    }

    public void pushAppointmentsToCalender(Activity curActivity, int eID,
            String title, String addInfo, String place, int status,
            long startDate, long endDate, boolean needReminder,
            boolean needOnTimeReminder) {

        try {
            String eventUriString = "content://com.android.calendar/events";
            ContentValues eventValues = new ContentValues();

            eventValues.put("calendar_id", 1);
            eventValues.put("title", title);
            eventValues.put("description", addInfo);
            eventValues.put("eventLocation", place);

            eventValues.put("dtstart", startDate);
            eventValues.put("dtend", endDate);
            eventValues.put("eventTimezone", getTimeZone());

            eventValues.put("eventStatus", status);
            eventValues.put("hasAlarm", 1);

            Uri eventUri = curActivity.getApplicationContext()
                    .getContentResolver()
                    .insert(Uri.parse(eventUriString), eventValues);
            long eventID = Long.parseLong(eventUri.getLastPathSegment());
            long reminderId = 0;

            try {
                saveCalenderId(String.valueOf(eID)   "_e", eventID);
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (needReminder) {
                String reminderUriString = "content://com.android.calendar/reminders";



                ContentValues reminderValues = new ContentValues();

                reminderValues.put("event_id", eventID);
                reminderValues.put("minutes", 120);
                reminderValues.put("method", 1);
                Uri reminderUri = curActivity.getApplicationContext()
                        .getContentResolver()
                        .insert(Uri.parse(reminderUriString), reminderValues);

                reminderId = Long.parseLong(reminderUri.getLastPathSegment());
                saveCalenderId(String.valueOf(eID)   "_r", reminderId);
            }

            if (needOnTimeReminder) {
                String reminderUriString = "content://com.android.calendar/reminders";

                ContentValues reminderValues = new ContentValues();

                reminderValues.put("event_id", eventID);
                reminderValues.put("minutes", 0);
                reminderValues.put("method", 0);
                Uri reminderUri = curActivity.getApplicationContext()
                        .getContentResolver()
                        .insert(Uri.parse(reminderUriString), reminderValues);

                reminderId = Long.parseLong(reminderUri.getLastPathSegment());
                saveCalenderId(String.valueOf(eID)   "_r", reminderId);
            }

            Log.e("save event", String.valueOf(eID)   "_e"   "-"   eventID);
            Log.e("save reminder", String.valueOf(eID)   "_r"   "-"
                      reminderId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String getTimeZone() {
        TimeZone tz = TimeZone.getDefault();
        return tz.getDisplayName(false, TimeZone.SHORT);
    }

    private void saveCalenderId(String eventIdKey, long eventIdValue) {
        SharedPreferences sharedpreferences = context.getSharedPreferences(
                "Lemeno", Context.MODE_PRIVATE);
        Editor editor = sharedpreferences.edit();
        editor.putLong(eventIdKey, eventIdValue);
        editor.commit();
    }

}

以及我如何称呼它

    CalendarSync cs = new CalendarSync(ViewEventActivity.this);
                cs.pushAppointmentsToCalender(
                        (Activity) ViewEventActivity.this, eventVO
                                .getEventId(), eventVO.getEventName(),
                        "Lemeno Event", eventVO.getLocationName(), 1,
                        System.currentTimeMillis(), new Timestamp(eventVO
                                .getEventDate().getTime()).getTime(), true,
                        false);

有什么帮助吗?

最佳答案:

0 个答案:

没有答案
本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复