2
votes

I'm developing an android app that creates a calendar on the phone the first time the application is installed. When I create an event, either programmatically or using the device itself, the event will show up in the calendar but its title is missing. I'm testing my code on HTC nexus one.

Since I'm getting the same behavior when creating the event through my code or through the device, I'm suspecting there is a problem in the code I wrote to create a new calendar account. I am doing so by adding a new field in the calendars table of the calendar.db database, whose URI is: "content://com.android.calendar/calendars"

 private Uri addNewCalendar() {

   if (!calendarExists()) {

       ContentResolver contentResolver = mContext.getContentResolver();

       ContentValues cv = new ContentValues();
       cv.put(Calendar.Calendars._SYNC_ACCOUNT, mAccount.name);
       cv.put(Calendar.Calendars._SYNC_ACCOUNT_TYPE, mAccount.type);
       cv.put(Calendar.Calendars._SYNC_TIME, new Date().getTime());
       cv.put(Calendar.Calendars.URL, "http://www.example.com");
       cv.put(Calendar.Calendars.NAME, mAccount.name);
       cv.put(Calendar.Calendars.DISPLAY_NAME, mAccount.name);
       cv.put(Calendar.Calendars.HIDDEN, 0);
       cv.put(Calendar.Calendars.COLOR, 14417920);
       cv.put(Calendar.Calendars.ACCESS_LEVEL, 700);
       cv.put(Calendar.Calendars.SELECTED, 1);
       cv.put(Calendar.Calendars.SYNC_EVENTS, 1);
       cv.put(Calendar.Calendars.TIMEZONE, "GMT");
       cv.put(Calendar.Calendars.OWNER_ACCOUNT, mAccount.name);

       Uri uri = contentResolver.insert(Calendar.Calendars.CONTENT_URI.buildUpon().build(), cv);

       return uri;
      }
      return null;
    }

Also, when checking whether the title field of the event is actually written in the database, I found it is saved correctly. I can see the event title when I try to edit the event on the phone.

Anyone has an idea why I'm having this problem? If you need more information, please let me know.

1
@user487750: Please use the Google Calendar GData API to manipulate a user's Google Calendar.CommonsWare
Thanks CommonsWare, but I have my own calendar server, and my android application is supposed to sync events from that server and save them to android's calendar application. I'm not using a Google calendar account. Is that doable with Google Calendar API?Moe

1 Answers

0
votes

Please try this code hope answer your Question:

  public int addEvent(String title, String location, String detail,
        long from, long to, int reminder, boolean update, int id) {
    ContentValues event = new ContentValues();
    Uri url = null;
    event.put("calendar_id", "1");
    // event.put("_id", id);
    event.put("title", title);
    event.put("description", detail);
    event.put("eventLocation", location);

    // startTime = System.currentTimeMillis();
    /*
     * try { Thread.sleep(5000); } catch (InterruptedException e) {
     * e.printStackTrace(); }
     */
    // endTime = System.currentTimeMillis();
    event.put("dtstart", startTime);
    event.put("dtend", endTime);
    event.put("allDay", 0); // 0 for false, 1 for true
    event.put("eventStatus", 1);
    event.put("visibility", 0);
    event.put("transparency", 0);
    event.put("hasAlarm", reminder); // 0 for false, 1 for true

    String contentProvider;
    if (Build.VERSION.RELEASE.contains("2.2")
            || Build.VERSION.RELEASE.contains("2.3"))
        contentProvider = "com.android.calendar";
    else
        contentProvider = "calendar";

    Uri remindersUri = Uri.parse(String.format("content://%s/reminders",
            contentProvider));
    Uri eventsUri = Uri.parse(String.format("content://%s/events",
            contentProvider));
    Uri calendars = Uri.parse(String.format("content://%s/calendars",
            contentProvider));
    if (update)
        getContentResolver().update(eventsUri, event, "event_id=?",
                new String[] {})/* ate(eventsUri, event) */;
    // retCode = db.update(SQLiteDBHandler.EVENT_TABLE, values, "_id=?", new
    // String[]{id+""});
    else
        url = getContentResolver().insert(eventsUri, event);
    String eventid = url.getPathSegments().get(
            url.getPathSegments().size() - 1);
    if (reminder == 1) {
        event = new ContentValues();
        event.put("event_id", eventid);
        event.put("minutes", reminder_time);
        event.put("method", 1);
        getContentResolver().insert(remindersUri, event);
        /*
         * try{ Cursor curs = getContentResolver().query(remindersUri, null,
         * null, null, null); curs.getColumnName(3); }catch (Exception e) {
         * // TODO: handle exception e.printStackTrace(); }
         */
    }
    return Integer.parseInt(eventid);
}