Created
April 14, 2012 09:56
-
-
Save vekexasia/2383248 to your computer and use it in GitHub Desktop.
Add Events on Google Calendar on Android Froyo and above
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Adds the event to a calendar. It lets the user choose the calendar | |
* @param ctx Context ( Please use the application context ) | |
* @param title title of the event | |
* @param dtstart Start time: The value is the number of milliseconds since Jan. 1, 1970, midnight GMT. | |
* @param dtend End time: The value is the number of milliseconds since Jan. 1, 1970, midnight GMT. | |
*/ | |
private static void addToCalendar(Context ctx, final String title, final long dtstart, final long dtend) { | |
final ContentResolver cr = ctx.getContentResolver(); | |
Cursor cursor ; | |
if (Integer.parseInt(Build.VERSION.SDK) >= 8 ) | |
cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null); | |
else | |
cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null); | |
if ( cursor.moveToFirst() ) { | |
final String[] calNames = new String[cursor.getCount()]; | |
final int[] calIds = new int[cursor.getCount()]; | |
for (int i = 0; i < calNames.length; i++) { | |
calIds[i] = cursor.getInt(0); | |
calNames[i] = cursor.getString(1); | |
cursor.moveToNext(); | |
} | |
AlertDialog.Builder builder = new AlertDialog.Builder(ctx); | |
builder.setSingleChoiceItems(calNames, -1, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
ContentValues cv = new ContentValues(); | |
cv.put("calendar_id", calIds[which]); | |
cv.put("title", title); | |
cv.put("dtstart", dtstart ); | |
cv.put("hasAlarm", 1); | |
cv.put("dtend", dtend); | |
Uri newEvent ; | |
if (Integer.parseInt(Build.VERSION.SDK) >= 8 ) | |
newEvent = cr.insert(Uri.parse("content://com.android.calendar/events"), cv); | |
else | |
newEvent = cr.insert(Uri.parse("content://calendar/events"), cv); | |
if (newEvent != null) { | |
long id = Long.parseLong( newEvent.getLastPathSegment() ); | |
ContentValues values = new ContentValues(); | |
values.put( "event_id", id ); | |
values.put( "method", 1 ); | |
values.put( "minutes", 15 ); // 15 minutes | |
if (Integer.parseInt(Build.VERSION.SDK) >= 8 ) | |
cr.insert( Uri.parse( "content://com.android.calendar/reminders" ), values ); | |
else | |
cr.insert( Uri.parse( "content://calendar/reminders" ), values ); | |
} | |
dialog.cancel(); | |
} | |
}); | |
builder.create().show(); | |
} | |
cursor.close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm sorry, but how may I generate my own Uri.parse content://com.android.calendar/calendars? May I add events to show on android devices using this code?
Thanks in advance Vekexasia!