使用'Intent.putExtra()'的方法添加提醒日历的事件 [英] Adding event with reminders to calendar with 'Intent.putExtra()' way of doing
问题描述
我正在尝试使用以下代码向日历添加事件:
public Intent calPopulation()
{
Intent calIntent = new Intent(Intent.ACTION_INSERT);
calIntent.setType(vnd.android.cursor.item / event);
calIntent.putExtra(CalendarContract.Events.TITLE,this._title);
GregorianCalendar calDate = new GregorianCalendar(this._year,this._month,this._day,this._hour,this._minute);
calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,calDate.getTimeInMillis());
calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,calDate.getTimeInMillis()+ 60 * 60 * 1000);
calIntent.putExtra(CalendarContract.Events.HAS_ALARM,true);
calIntent.putExtra(CalendarContract.Reminders.EVENT_ID,CalendarContract.Events._ID);
calIntent.putExtra(CalendarContract.Events.ALLOWED_REMINDERS,METHOD_DEFAULT);
calIntent.putExtra(CalendarContract.Reminders.METHOD,CalendarContract.Reminders.METHOD_ALERT);
calIntent.putExtra(CalendarContract.Reminders.MINUTES,5);
return calIntent;
}
然后启动该操作: startActivity(mTask我没有任何问题,日历应用程序事件的启动与正确的信息我输入到我的应用程序,除了它不填写我想添加的提醒的事件。
你有什么线索吗?我试图在许多线程中使用这种方法搜索(我的意思是intent.putExtra),但从来没有发现任何有趣的东西。
其他的一点是,有没有办法直接保存事件+提醒进入日历而不打开日历应用程序并请求用户操作?
提前感谢。
Alex。
根据我指出的方法没有回答,但我发现添加事件的另一种方式提醒日历。
以下方法对我有用:
//将事件添加到用户的日历。
public void addEvent(Context context){
GregorianCalendar calDate = new GregorianCalendar(this._year,this._month,this._day,this._hour,this._minute);
try {
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART,calDate.getTimeInMillis());
values.put(CalendarContract.Events.DTEND,calDate.getTimeInMillis()+ 60 * 60 * 1000);
values.put(CalendarContract.Events.TITLE,this._title);
values.put(CalendarContract.Events.CALENDAR_ID,1);
values.put(CalendarContract.Events.EVENT_TIMEZONE,Calendar.getInstance()
.getTimeZone()。getID());
System.out.println(Calendar.getInstance()。getTimeZone()。getID());
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI,values);
//将eventId保存到Task对象中,以备将来删除。
this._eventId = Long.parseLong(uri.getLastPathSegment());
//添加5分钟,1小时和1天提醒(3个提醒)
setReminder(cr,this._eventId,5);
setReminder(cr,this._eventId,60);
setReminder(cr,this._eventId,1440);
} catch(Exception e){
e.printStackTrace();
}
}
//添加事件事件的例程
public void setReminder(ContentResolver cr,long eventID,int timeBefore){
try {
ContentValues values = new ContentValues();
values.put(CalendarContract.Reminders.MINUTES,timeBefore);
values.put(CalendarContract.Reminders.EVENT_ID,eventID);
values.put(CalendarContract.Reminders.METHOD,CalendarContract.Reminders.METHOD_ALERT);
Uri uri = cr.insert(CalendarContract.Reminders.CONTENT_URI,values);
Cursor c = CalendarContract.Reminders.query(cr,eventID,
new String [] {CalendarContract.Reminders.MINUTES});
if(c.moveToFirst()){
System.out.println(calendar
+ c.getInt(c.getColumnIndex(CalendarContract.Reminders.MINUTES)));
}
c.close();
} catch(Exception e){
e.printStackTrace();
}
}
//使用存储在Task对象中的eventId从日历中删除事件的功能。
public void removeEvent(Context context){
ContentResolver cr = context.getContentResolver();
int iNumRowsDeleted = 0;
Uri eventsUri = Uri.parse(CALENDAR_URI_BASE +events);
Uri eventUri = ContentUris.withAppendedId(eventsUri,this._eventId);
iNumRowsDeleted = cr.delete(eventUri,null,null);
Log.i(DEBUG_TAG,已删除+ iNumRowsDeleted +日历条目);
}
public int updateEvent(Context context){
int iNumRowsUpdated = 0;
GregorianCalendar calDate = new GregorianCalendar(this._year,this._month,this._day,this._hour,this._minute);
ContentValues event = new ContentValues();
event.put(CalendarContract.Events.TITLE,this._title);
event.put(hasAlarm,1); // 0为false,1为true
event.put(CalendarContract.Events.DTSTART,calDate.getTimeInMillis());
event.put(CalendarContract.Events.DTEND,calDate.getTimeInMillis()+ 60 * 60 * 1000);
Uri eventsUri = Uri.parse(CALENDAR_URI_BASE +events);
Uri eventUri = ContentUris.withAppendedId(eventsUri,this._eventId);
iNumRowsUpdated = context.getContentResolver()。update(eventUri,event,null,
null);
// TODO将文本放入strings.xml
Log.i(DEBUG_TAG,已更新+ iNumRowsUpdated +日历条目);
return iNumRowsUpdated;
}
希望这可以帮助遇到同样问题的其他人:) / p>
Alex。
I'm trying to add events to calendar with the following code :
public Intent calPopulation()
{
Intent calIntent = new Intent(Intent.ACTION_INSERT);
calIntent.setType("vnd.android.cursor.item/event");
calIntent.putExtra(CalendarContract.Events.TITLE, this._title);
GregorianCalendar calDate = new GregorianCalendar(this._year,this._month, this._day, this._hour, this._minute);
calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, calDate.getTimeInMillis());
calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, calDate.getTimeInMillis()+60*60*1000);
calIntent.putExtra(CalendarContract.Events.HAS_ALARM, true);
calIntent.putExtra(CalendarContract.Reminders.EVENT_ID, CalendarContract.Events._ID);
calIntent.putExtra(CalendarContract.Events.ALLOWED_REMINDERS, "METHOD_DEFAULT");
calIntent.putExtra(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
calIntent.putExtra(CalendarContract.Reminders.MINUTES,5);
return calIntent;
}
And then launch the action with : startActivity(mTask.calPopulation());
I don't have any issue, the calendar app event launched with the correct information I entered into my app, except that it does not fill in the event the reminder I would like to add.
Do you have any clues? I tried to search within many threads using this method (I mean the intent.putExtra) but never find anything interesting.
Other point, is there a way to directly save the event + reminder into the calendar without opening calendar App and requesting for user action?
Thanks in advance. Alex.
No answer according to the method I pointed out, but another way I found to add event with reminders to the calendar.
Hereunder methods are working fine for me :
// Add an event to the calendar of the user.
public void addEvent(Context context) {
GregorianCalendar calDate = new GregorianCalendar(this._year, this._month, this._day, this._hour, this._minute);
try {
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, calDate.getTimeInMillis());
values.put(CalendarContract.Events.DTEND, calDate.getTimeInMillis()+60*60*1000);
values.put(CalendarContract.Events.TITLE, this._title);
values.put(CalendarContract.Events.CALENDAR_ID, 1);
values.put(CalendarContract.Events.EVENT_TIMEZONE, Calendar.getInstance()
.getTimeZone().getID());
System.out.println(Calendar.getInstance().getTimeZone().getID());
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
// Save the eventId into the Task object for possible future delete.
this._eventId = Long.parseLong(uri.getLastPathSegment());
// Add a 5 minute, 1 hour and 1 day reminders (3 reminders)
setReminder(cr, this._eventId, 5);
setReminder(cr, this._eventId, 60);
setReminder(cr, this._eventId, 1440);
} catch (Exception e) {
e.printStackTrace();
}
}
// routine to add reminders with the event
public void setReminder(ContentResolver cr, long eventID, int timeBefore) {
try {
ContentValues values = new ContentValues();
values.put(CalendarContract.Reminders.MINUTES, timeBefore);
values.put(CalendarContract.Reminders.EVENT_ID, eventID);
values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
Uri uri = cr.insert(CalendarContract.Reminders.CONTENT_URI, values);
Cursor c = CalendarContract.Reminders.query(cr, eventID,
new String[]{CalendarContract.Reminders.MINUTES});
if (c.moveToFirst()) {
System.out.println("calendar"
+ c.getInt(c.getColumnIndex(CalendarContract.Reminders.MINUTES)));
}
c.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// function to remove an event from the calendar using the eventId stored within the Task object.
public void removeEvent(Context context) {
ContentResolver cr = context.getContentResolver();
int iNumRowsDeleted = 0;
Uri eventsUri = Uri.parse(CALENDAR_URI_BASE+"events");
Uri eventUri = ContentUris.withAppendedId(eventsUri, this._eventId);
iNumRowsDeleted = cr.delete(eventUri, null, null);
Log.i(DEBUG_TAG, "Deleted " + iNumRowsDeleted + " calendar entry.");
}
public int updateEvent(Context context) {
int iNumRowsUpdated = 0;
GregorianCalendar calDate = new GregorianCalendar(this._year, this._month, this._day, this._hour, this._minute);
ContentValues event = new ContentValues();
event.put(CalendarContract.Events.TITLE, this._title);
event.put("hasAlarm", 1); // 0 for false, 1 for true
event.put(CalendarContract.Events.DTSTART, calDate.getTimeInMillis());
event.put(CalendarContract.Events.DTEND, calDate.getTimeInMillis()+60*60*1000);
Uri eventsUri = Uri.parse(CALENDAR_URI_BASE+"events");
Uri eventUri = ContentUris.withAppendedId(eventsUri, this._eventId);
iNumRowsUpdated = context.getContentResolver().update(eventUri, event, null,
null);
// TODO put text into strings.xml
Log.i(DEBUG_TAG, "Updated " + iNumRowsUpdated + " calendar entry.");
return iNumRowsUpdated;
}
Hope this could help others that encountered the same problem I had :).
Alex.
这篇关于使用'Intent.putExtra()'的方法添加提醒日历的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!