4

I'm writing an app that needs to write events to a calendar in the background, without input from the user once the app has been setup. This means that the methods where the event is created using Intents is not what I am after.

I found this code on a website, but it doesn't work:

    long start = 1297512000; // 2011-02-12 12h00
    long end = 1297515600;   // 2011-02-12 13h00

    String title = "TEST ENTRY - DELETE ME!!"; 

    ContentValues cvEvent = new ContentValues();
    cvEvent.put("calendar_id", 1);
    cvEvent.put("title", title);
    cvEvent.put("dtstart", start );
    //cvEvent.put("hasAlarm", 1);
    cvEvent.put("dtend", end);

    getContentResolver().insert(Uri.parse("content://com.android.calendar/events"), cvEvent);

Now, before anybody wades in with the "Google HIGHLY recommend you don't use this method" or such like, I already know this.

Has anyone got this working? I'm running Gingerbread on my device, so that may cause an issue!

The only alternative I've found is to use the GData APIs, but then I need to authenticate which is why the above is better.... there are apps out there that need no authentication so they must be using this too! :)

If anyone can help, please do!

Thanks Neil

neildeadman
  • 3,974
  • 13
  • 42
  • 55
  • strangely, using an online epoch converter the dates above worked correctly. I just discovered that these are actually wrong and are for a date in 1970. To correct them I need to add 3x 0 to the end so they become: 1297512000000 and 1297515600000. My code was working but the dates were wrong so I never saw events being added! DOH! – neildeadman Feb 12 '11 at 16:51
  • using the same sites bulk converter shows correct dates though... I have contacted author of that site. – neildeadman Feb 12 '11 at 16:53

2 Answers2

1

There was a problem with the dates when converted. I was ending up with a date in 1970s but expected a Feb 2011 date.

Using a different epoch converter showed I had 3 missing 0's.

neildeadman
  • 3,974
  • 13
  • 42
  • 55
0

I had an app that did this but both HTC Sense UI and Motorola Motoblur broke this functionality on many phones, with no fix that I could ever find. It's a real shame that Google can't button up these APIs and make them properly available on all devices. It seems like basic functionality that should just be there in a mobile device SDK. I know it's there in the Blackberry SDK.

I'd LOVE to be able to do this, so if you find a way please post it here so that I can see it. However I'd caution against spending a lot of time on this, as I just don't think you can get this to work on anything but a small subset of Android devices out there.

Mark B
  • 183,023
  • 24
  • 297
  • 295