我想在我的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小时的时间。
有人能说我的代码出错了吗?
答案 0 :(得分:5)
如果您获得如下Date = May 05, 2012, 07:10PM
的数据,则将日期转换为数字格式为5月为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);
答案 1 :(得分:2)
我们还可以将事件添加到日历中,而不是传递意图,这看起来很酷
public static String 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();
}
}
希望,这肯定会对你有所帮助:)。
谢谢
答案 2 :(得分: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);
希望它可以帮助你:)
谢谢
答案 3 :(得分: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);
希望这会对你有所帮助。
...谢谢