2

我正在尝试在我的 android 日历事件中添加以下日期和时间....

Title and description =  Events for test
Start Date = May 05, 2012, 07:10PM
End Date = May 06, 2012, 02:10PM

以下是我添加到事件的代码

                    long startTime = 0, endTime = 0;
                    Date d = new Date();
            startTime = d.parse(start);
            d.setTime(startTime);

            endTime = d.parse(end);
            d.setTime(endTime);

            Intent intent = new Intent(Intent.ACTION_EDIT);
            intent.setType("vnd.android.cursor.item/event");
            intent.putExtra(Events.TITLE, summary);
            intent.putExtra(Events.DESCRIPTION, summary);
            intent.putExtra(Events.EVENT_LOCATION, "");
            intent.putExtra(Events.DTSTART, startTime);
            intent.putExtra(Events.DTEND, endTime);
            intent.putExtra(Events.ALL_DAY, allDayFlag);
            intent.putExtra(Events.STATUS, 1);
            intent.putExtra(Events.VISIBLE, 0);
            intent.putExtra(Events.HAS_ALARM, 1);
            startActivity(intent);

但是在“事件详细信息”页面中,我得到了标题,仅描述是正确的。在开始和结束日期的地方,我得到了当前日期,其中开始和结束时间是下一个 1 小时时间。

谁能告诉我我的代码出了什么问题.....

4

4 回答 4

5

如果您按如下方式获取数据Date = May 05, 2012, 07:10PM,则将日期转换为数字格式 a May 为 05 并在以下代码中传递值。

Calendar beginCal = Calendar.getInstance();
                beginCal.set(year, mnth, day, hrs, min);
                startTime = beginCal.getTimeInMillis();

                Calendar endCal = Calendar.getInstance();
                endCal.set(year, mnth, day, hrs, min);
                endTime = endCal.getTimeInMillis();     

                Intent intent = new Intent(Intent.ACTION_INSERT);
                intent.setType("vnd.android.cursor.item/event");
                intent.putExtra(Events.TITLE, summary);
                intent.putExtra(Events.DESCRIPTION, summary);
                intent.putExtra(Events.EVENT_LOCATION, "");     
                intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginCal.getTimeInMillis());
                intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endCal.getTimeInMillis());
                intent.putExtra(Events.ALL_DAY, allDayFlag);
                intent.putExtra(Events.STATUS, 1);
                intent.putExtra(Events.VISIBLE, 0);
                intent.putExtra(Events.HAS_ALARM, 1);
                startActivity(intent);
于 2012-05-12T09:14:57.883 回答
2

除了传递意图,我们还可以像这样将事件添加到日历中,这看起来很酷

公共静态字符串 getCalendarUriBase() {

    String calendarUriBase = null;
    Uri calendars = Uri.parse("content://calendar/calendars");
    Cursor managedCursor = null;
    try {
        managedCursor = act.managedQuery(calendars, null, null, null, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (managedCursor != null) {
        calendarUriBase = "content://calendar/";
    } else {
        calendars = Uri.parse("content://com.android.calendar/calendars");
        try {
            managedCursor = act.managedQuery(calendars, null, null, null,
                    null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (managedCursor != null) {
            calendarUriBase = "content://com.android.calendar/";
        }
    }
    // //Log.d("Calendar", "" + calendarUriBase);
    return calendarUriBase;
}

main() {

    DateFormat formatter = new SimpleDateFormat("MMMM dd, yyyy, h:mma");
    String dateStarts = "May 05, 2012, 07:10PM";
    String dateEnds = "May 06, 2012, 02:10PM";
    Uri EVENTS_URI = Uri.parse(getCalendarUriBase() + "events");
    ContentResolver cr = getActivity().getContentResolver();
    // event insert
    ContentValues values = new ContentValues();
    Date startDate, endDate;
    startDate = formatter.parse(dateStarts);
    endDate = formatter.parse(dateEnds);
    values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
    values.put("calendar_id", 1);
    values.put("title", remainderTitle);
    values.put("allDay", 0);
    values.put("dtstart", startDate.getTime());
    values.put("dtend", enddDate.getTime());
    values.put("description", remainderInfo);
    values.put("visibility", 0);
    values.put("hasAlarm", 1);// now
    try {
        cr.insert(EVENTS_URI, values);
        // reminder insert
        Uri REMINDERS_URI = Uri.parse(getCalendarUriBase()
                + "reminders");
        values = new ContentValues();
        values.put("event_id", 1);
        values.put("method", 1);
        values.put("minutes", 10);
        cr.insert(REMINDERS_URI, values);

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

}

希望,它肯定会帮助你:)

谢谢

于 2013-01-18T09:47:09.333 回答
2

可能是android Sdk问题,请试试这个

    int SDK_INT = android.os.Build.VERSION.SDK_INT;
    DateFormat formatter = new SimpleDateFormat("MMMM dd, yyyy, h:mmaa");
    long lnsTime = 0, lneTime = 0;

    Date dateObject;

    try {
        String dob_var = "May 05, 2012, 07:10PM";

        dateObject = formatter.parse(dob_var);

        lnsTime = dateObject.getTime();
        Log.e(null, Long.toString(lnsTime));

        dob_var = "May 06, 2012, 02:10PM";

        dateObject = formatter.parse(dob_var);

        lneTime = dateObject.getTime();
        Log.e(null, Long.toString(lneTime));
    }

    catch (java.text.ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.i("E11111111111", e.toString());
    }

    Intent intent = new Intent(Intent.ACTION_EDIT);
    intent.setType("vnd.android.cursor.item/event");
    if (SDK_INT >= 15) {
        intent.putExtra(Events.CALENDAR_ID, 1);
        intent.putExtra(Events.TITLE, "Title");
        intent.putExtra(Events.DESCRIPTION, "Title");
        intent.putExtra(Events.DTSTART, lnsTime);
        intent.putExtra(Events.DTEND, lneTime);
        intent.putExtra(Events.ALL_DAY, 0);
        intent.putExtra(Events.STATUS, 1);
        intent.putExtra(Events.VISIBLE, 0);
        intent.putExtra(Events.HAS_ALARM, 1);
    } else {
        intent.putExtra("calendar_id", 1);
        intent.putExtra("status", 1);
        intent.putExtra("visibility", 0);
        intent.putExtra("hasAlarm", 1);
        intent.putExtra("beginTime", lnsTime);
        intent.putExtra("allDay", true);
        intent.putExtra("rrule", "FREQ=YEARLY");
        intent.putExtra("endTime", lneTime);
        intent.putExtra("title", "Title");
        intent.putExtra("description", "Title");
    }
    startActivity(intent);

希望它可以帮助你:)

谢谢

于 2013-03-25T08:23:36.623 回答
0

我认为可能是您的日期解析导致此问题。

尝试这个

DateFormat formatter = new SimpleDateFormat("MMMM dd, yyyy, h:mmaa"); 
long lnsTime, lneTime;

        Date dateObject;

        try{
        String dob_var = "May 05, 2012, 07:10PM";

        dateObject = formatter.parse(dob_var);

        lnsTime = dateObject.getTime();
        Log.e(null, Long.toString(lnsTime));

        dob_var = "May 06, 2012, 02:10PM";      

        dateObject = formatter.parse(dob_var);

        lneTime = dateObject.getTime();
        Log.e(null, Long.toString(lneTime));
        }

        catch (java.text.ParseException e) 
            {
            // TODO Auto-generated catch block
                e.printStackTrace();
                Log.i("E11111111111", e.toString());
            }

     Intent intent = new Intent(Intent.ACTION_EDIT);
            intent.setType("vnd.android.cursor.item/event");
            intent.putExtra(Events.TITLE, summary);
            intent.putExtra(Events.DESCRIPTION, summary);
            intent.putExtra(Events.EVENT_LOCATION, "");
            intent.putExtra(Events.DTSTART, lnsTime);
            intent.putExtra(Events.DTEND, lneTime);
            intent.putExtra(Events.ALL_DAY, allDayFlag);
            intent.putExtra(Events.STATUS, 1);
            intent.putExtra(Events.VISIBLE, 0);
            intent.putExtra(Events.HAS_ALARM, 1);
            startActivity(intent);

希望这会帮助你。

谢谢...

于 2012-05-02T12:13:16.080 回答