There are many examples on how to create a new calendar event in android but none on how to open and display an event. This is my code so far
public static void startCalendarMimeType(Context context, CalendarItem item){
//all version of android
Intent i = new Intent();
// mimeType will popup the chooser any for any implementing application (e.g. the built in calendar or applications such as "Business calendar"
i.setType("vnd.android.cursor.item/event");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// the time the event should start in millis. This example uses no开发者_运维问答w as the start time and ends in 1 hour
//i.putExtra("beginTime", item.getBegin());
//i.putExtra("endTime", item.getEnd());
i.putExtra("_id", item.getId());
// the action
//i.setAction(Intent.ACTION_PICK);
context.startActivity(i);
}
The Calendar item contains information already retrieved from the calendar using the content resolver. When a user clicks on my item I want it to open the Android calendar displaying the item.
At this point you can select an app to open with, If you choose "Show Event" it does open the calendar app but gets a nullpointer exception and I just can't figure out what I'm doing wrong here. I'm I the first who try's to do this?
Any help very much appreciated
I finally found the solution:
Intent intent = new Intent(Intent.ACTION_VIEW);
//Android 2.2+
intent.setData(Uri.parse("content://com.android.calendar/events/" + String.valueOf(calendarEventID)));
//Android 2.1 and below.
//intent.setData(Uri.parse("content://calendar/events/" + String.valueOf(calendarEventID)));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NO_HISTORY
| Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
context.startActivity(intent);
I hope that some of you find this usefull.
I have also added a few other calendar Intents below:
/**
* Add a calendar event.
*/
private void addCalendarEvent(){
Context context = getContext();
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NO_HISTORY
| Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
context.startActivity(intent);
}
/**
* Edit a calendar event.
*/
private void editCalendarEvent(){
Context context = getContext();
long calendarEventID = .....
intent.setData(Uri.parse("content://com.android.calendar/events/" + String.valueOf(calendarEventID)));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NO_HISTORY
| Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
context.startActivity(intent);
}
Let me know if anyone has any questions or has a better way to accomplish the same task.
There are many examples on how to create a new calendar event in android
None of those "examples" should be used, as there is no documented and supported API for a calendar in Android.
but none on how to open and display an event
You would need to contact the authors of whatever third-party calendar program you are trying to integrate with and ask them how to do that integration. If you are trying to integrate with the Calendar application that is part of the Android open source project, there is no documented and supported API for that application.
精彩评论