Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding events to android calendar failing
    text
    copied!<p>I am trying to add events to a user's calendar programatically. I have followed countless guides here on StackOverflow and elsewhere, but none of them will make that damned event show up in my calendar. Here is my current code:</p> <pre><code>public void saveGamesToCalendar() { showCalendarPopup(); } private void showCalendarPopup() { final ContentResolver cr; final Cursor result; final Uri uri; List&lt;String&gt; listCals = new ArrayList&lt;String&gt;(); final String[] projection = new String[] {CalendarContract.Calendars._ID, CalendarContract.Calendars.ACCOUNT_NAME, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, CalendarContract.Calendars.NAME,}; uri = CalendarContract.Calendars.CONTENT_URI; cr = context.getContentResolver(); result = cr.query(uri, projection, null, null, null); if(result.getCount() &gt; 0 &amp;&amp; result.moveToFirst()) { do { listCals.add(result.getString(result.getColumnIndex(CalendarContract.Calendars.NAME))); } while(result.moveToNext()); } CharSequence[] calendars = listCals.toArray(new CharSequence[listCals.size()]); AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("Calendar to use:"); builder.setItems(calendars, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog,int itemCal) { Log.e("SettingsActivity", "CalendarID: " + itemCal); loopThroughGames(itemCal); }; }); AlertDialog alert = builder.create(); alert.show(); } private void loopThroughGames(int whichCalendar) { ArrayList&lt;Model_Game&gt; games = memoryManager.getGames(); // for(Game e: games) Log.e("Game", games.get(101).toString()); addGameToCalendar(games.get(101), whichCalendar); Log.e("ID", "" + whichCalendar); } private void addGameToCalendar(Model_Game game,int whichCalendar) { ContentResolver cr = context.getContentResolver(); ContentValues values = new ContentValues(); try { values.put(CalendarContract.Events.CALENDAR_ID, whichCalendar); values.put(CalendarContract.Events.TITLE, "Hockey- " + game.getLeague() + ": " + game.getTeamH() + " vs " + game.getTeamA()); values.put(CalendarContract.Events.EVENT_LOCATION, "Twin Rinks Ice Arena- " + game.getRink() + " Rink"); values.put(CalendarContract.Events.DTSTART, "" + game.getCalendarObject().getTimeInMillis()); //values.put(CalendarContract.Events.DTEND, "" + game.getCalendarObject().getTimeInMillis() + 5400000); values.put(CalendarContract.Events.DURATION, "" + 5400000); values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID()); cr.insert(CalendarContract.Events.CONTENT_URI, values); } catch(Exception e) { Log.e("Error", e.getMessage()); toast(e.getMessage()); } } </code></pre> <p>Basically what is going on is that the user presses an action bar button, which calls the <code>saveGamesToCalendar()</code> method, which calls the <code>ShowCalendarPopup()</code> method. This method shows a standard dialog with the list of the user's calendars, and when the user selects one of them, the <code>loopThroughGames()</code> method is called with the ID of the chosen calendar as a parameter. This method will, when everything is working, write all the user's games to the calendar, but for testing purposes it only writes one. The <code>addGameToCalendar()</code> method takes a game object, which holds lots of values like time, title, location, date, etc., and inserts it into the calendar using the standard way outlined in many different places on the web.</p> <p>I am getting no error messages with this code (other than the ones I send to the error log myself) so I have no idea why this code isn't working. No errors, just the games never show up in my calendar. I have the permissions set correctly in the manifest so I know that isn't the problem.</p> <p>Does anyone have a solution to this annoying problem? Thanks for your help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload