Getting started with the calendar

I was just trying to start Calendar Activity from my Activity . I have the following code in Button OnClickListener :

 Intent calIntent = new Intent(Intent.ACTION_INSERT); calIntent.setData(Uri.parse("content://com.android.calendar/events/")); startActivity(calIntent); 

But when I click Button , my device freezes, and then I am forced to close the closing application.

+4
source share
5 answers

This is because Calendar Uri content varies for different versions (APIs) for android. Try this code to get the Uri calendar for the corresponding API level.

 /* * Determines if it a pre 2.1 or a 2.2 calendar Uri, and returns the Uri */ private String getCalendarUriBase(Context con) { String calendarUriBase = null; Uri calendars = Uri.parse("content://calendar/calendars"); Cursor managedCursor = null; try { managedCursor = managedQuery(calendars, null, null, null, null); } catch (Exception e) { // eat } if (managedCursor != null) { calendarUriBase = "content://calendar/"; } else { calendars = Uri.parse("content://com.android.calendar/calendars"); try { managedCursor = managedQuery(calendars, null, null, null, null); } catch (Exception e) { // statement to print the stacktrace } if (managedCursor != null) { calendarUriBase = "content://com.android.calendar/"; } } return calendarUriBase; } 
+1
source

This is my personal CalendarOrganizer class, they changed how a calendar can be accessed from an ice cream sandwich, because before an ice cream sandwich, it is recommended to use their online services to update the calendar, since the Google calendar can be changed or not even set.

Edit : I found out that I need to deal with the problems of intent, but also that some phones on the ice cream sandwich will come out of Intent.ACTION_INSERT, but not Intent.ACTION_EDIT. So I updated my implementation. Thanks to this post for a solution.

 import android.content.Context; import android.content.Intent; import android.provider.CalendarContract; import android.provider.CalendarContract.Events; public class CalendarOrganizer { private final static int ICE_CREAM_BUILD_ID = 14; /** * Creates a calendar intent going from startTime to endTime * @param startTime * @param endTime * @param context * @return true if the intent can be handled and was started, * false if the intent can't be handled */ public static boolean createEvent(long startTime, long endTime, String title, String description, String location, boolean isAllDay, Context context) { int sdk = android.os.Build.VERSION.SDK_INT; if(sdk < ICE_CREAM_BUILD_ID) { // all SDK below ice cream sandwich Intent intent = new Intent(Intent.ACTION_EDIT); intent.setType("vnd.android.cursor.item/event"); intent.putExtra("beginTime", startTime); intent.putExtra("endTime", endTime); intent.putExtra("title", title); intent.putExtra("description", description); intent.putExtra("eventLocation", location); intent.putExtra("allDay", isAllDay); // intent.putExtra("rrule", "FREQ=YEARLY"); try { context.startActivity(intent); return true; } catch(Exception e) { return false; } } else { // ice cream sandwich and above Intent intent = new Intent(Intent.ACTION_EDIT); intent.setType("vnd.android.cursor.item/event"); intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startTime); intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime); intent.putExtra(Events.TITLE, title); intent.putExtra(Events.ACCESS_LEVEL, Events.ACCESS_PRIVATE); intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY , isAllDay); intent.putExtra(Events.DESCRIPTION, description); intent.putExtra(Events.EVENT_LOCATION, location); // intent.putExtra(Events.RRULE, "FREQ=DAILY;COUNT=10") try { context.startActivity(intent); return true; } catch(Exception e) { return false; } } } } 
+1
source

Here is the code that worked for me (using> v4):

 Uri uri = Uri.parse("content://com.android.calendar/events"); Intent calIntent = new Intent("android.intent.action.INSERT", uri) .setAction(Intent.ACTION_INSERT); startActivity(calIntent); 
0
source

For the record, ICS calendar intentions are described here.

http://developer.android.com/guide/topics/providers/calendar-provider.html#intents

0
source

Here is the code that worked for me

No need to create activity in the manifest.

Will work with all Android platforms.

Show you a calendar view of the current month

  long epoch = new Date.getTime(); Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon(); builder.appendPath("time"); ContentUris.appendId(builder, epoch); Intent intent = new Intent(Intent.ACTION_VIEW) .setData(builder.build()); startActivity(intent); 
0
source

All Articles