Android Calendar timezone not changing with CalendarContract

扶醉桌前 提交于 2019-12-23 01:51:17

问题


I am using the following code to add an event to the calender

    Intent intent = new Intent(Intent.ACTION_INSERT);
    intent.setType("vnd.android.cursor.item/event");
    intent.putExtra(Events.TITLE, "my event title");
    intent.putExtra(Events.EVENT_LOCATION, "my city");
    intent.putExtra(Events.DESCRIPTION, "description of this event");

    intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, dep.getTimeInMillis());
    intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,  arr.getTimeInMillis());        

    intent.putExtra(CalendarContract.Events.EVENT_TIMEZONE, departureTimeZone);
    intent.putExtra(CalendarContract.Events.EVENT_END_TIMEZONE, arrivalTimeZone);

    // Making it private and shown as busy
    intent.putExtra(Events.ACCESS_LEVEL, Events.ACCESS_PRIVATE);
    intent.putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    CoreApplication.getContext().startActivity(intent);

The problem is that the time it picks up is One Hour SHORTER than the original time i send and timezone is always set to device's timezone like "Central European Summer Time GMT +2"

HELP PLEASE!!!!!!!!!!!!!!!


回答1:


Had the same trouble but found a way out.
TimeZone sets to the device's timeZone by default. To change this and to set it to a specific timeZone use the getRawOffset() property.
This method calculates the milliseconds from the current time. So you can add the milliseconds for your specified timeZone and subtract those for the default timeZone.

When I tried to change it to timeZone 'GMT_ID'

values.put(CalendarContract.Events.DTSTART, startDate.getTime()  +TimeZone.getTimeZone(GMT_ID).getRawOffset() -TimeZone.getDefault().getRawOffset());

Hope this helps.




回答2:


You should check this link Calendar Provider intents, it says that we can't put timezone as intent extras, so send the beginTime and endTime of the event in UTC.

Mentioned in the example of event insert intent, "It uses the CalendarContract.EXTRA_EVENT_BEGIN_TIME and CalendarContract.EXTRA_EVENT_END_TIME extra fields to pre-populate the form with the time of the event. The values for these times must be in UTC milliseconds from the epoch."



来源:https://stackoverflow.com/questions/19002337/android-calendar-timezone-not-changing-with-calendarcontract

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!