Created
July 31, 2014 19:17
-
-
Save XinyueZ/cbcaaea28cea915a05af to your computer and use it in GitHub Desktop.
Insert an event in to the calendar on device without showing UI.
This file contains 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
//See more on android source directly. https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/provider/CalendarContract.java | |
/* | |
* Util functions for accessing calendar services of Android. Although the App | |
* supports >= 2.3.3, we still show here the low-level URIs for calendar | |
* services, so that we can learn and reuse them in other projects. | |
* | |
* Inspired by | |
* https://android.googlesource.com/platform/frameworks/base/+/refs/heads | |
* /master/core/java/android/provider/CalendarContract.java | |
* | |
* | |
* There is a non-official method that an Intent could be sent and the Calendar | |
* formula(activity) will be opened. The users have to input more information | |
* manually. The requirement doesn't want. | |
* | |
* | |
Intent intent = new Intent(Intent.ACTION_EDIT); | |
intent.setType("vnd.android.cursor.item/event"); | |
intent.putExtra("beginTime", start); | |
intent.putExtra("allDay", false); | |
intent.putExtra("endTime", end); | |
intent.putExtra("title", _title); | |
intent.putExtra("description", _description); | |
_context.startActivity(intent); | |
* | |
* | |
*/ | |
public final class CalendarUtil { | |
public interface OnAccessingCalanderListener { | |
void onPanic(); | |
void onEnd(); | |
} | |
public interface CalendarObserverCallback { | |
void onCalendarRefreshed(); | |
} | |
private final static String CALANDER_URL; | |
private final static String CALANDER_EVENT_URL; | |
private final static String CALANDER_REMIDER_URL; | |
static { | |
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO) { | |
CALANDER_URL = "content://com.android.calendar/calendars"; | |
CALANDER_EVENT_URL = "content://com.android.calendar/events"; | |
CALANDER_REMIDER_URL = "content://com.android.calendar/reminders"; | |
} else { | |
CALANDER_URL = "content://calendar/calendars"; | |
CALANDER_EVENT_URL = "content://calendar/events"; | |
CALANDER_REMIDER_URL = "content://calendar/reminders"; | |
} | |
} | |
private static WeakReference<CalendarObserverCallback> sCalendarObserverCallback; | |
private static ContentObserver sContentObserver; | |
private static byte[] LOCK = new byte[0]; | |
private static Cursor sEventCursor; | |
private static String getGoogleID(Context _context) { | |
String calId = null; | |
Cursor userCursor = _context.getContentResolver().query(Uri.parse(CALANDER_URL), null, null, null, null); | |
if (userCursor.getCount() > 0) { | |
userCursor.moveToFirst(); | |
calId = userCursor.getString(userCursor.getColumnIndex("_id")); | |
return calId; | |
} | |
return null; | |
} | |
public static void addEvent(Context _context, int _eventID, String _title, String _description, String _date, | |
int _start, int _duration, OnAccessingCalanderListener _onAccessingCalanderListener) { | |
String googleID = getGoogleID(_context); | |
if (googleID == null) { | |
_context.startActivity(new Intent(Settings.ACTION_ADD_ACCOUNT)); | |
if (_onAccessingCalanderListener != null) { | |
_onAccessingCalanderListener.onPanic(); | |
} | |
} else { | |
try { | |
// Make an event | |
ContentValues event = new ContentValues(); | |
event.put("title", _title); | |
event.put("description", _description); | |
event.put("calendar_id", googleID); | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTime(new SimpleDateFormat("dd.MM.yyyy", Locale.getDefault()).parse(_date)); | |
calendar.set(Calendar.HOUR_OF_DAY, _start); | |
long start = calendar.getTime().getTime(); | |
calendar.set(Calendar.HOUR_OF_DAY, _start + _duration); | |
long end = calendar.getTime().getTime(); | |
event.put("dtstart", start); | |
event.put("dtend", end); | |
event.put("hasAlarm", 1); | |
// http://stackoverflow.com/questions/11868433/error-in-calendar-application-for-insert-events | |
event.put("eventTimezone", TimeZone.getDefault().getID()); | |
// Insert the event | |
Uri newEvent = _context.getContentResolver().insert(Uri.parse(CALANDER_EVENT_URL), event); | |
long id = Long.parseLong(newEvent.getLastPathSegment()); | |
ContentValues values = new ContentValues(); | |
values.put("event_id", id); | |
values.put("minutes", 10); | |
_context.getContentResolver().insert(Uri.parse(CALANDER_REMIDER_URL), values); | |
if (_onAccessingCalanderListener != null) { | |
_onAccessingCalanderListener.onEnd(); | |
} | |
// CalendarToast toast = new | |
// CalendarToast(_context);toast.show(); | |
ECEToast.showInstance(_context, ECEToast.ToastType.ADD_EVENT); | |
} catch (Exception _e) { | |
if (_onAccessingCalanderListener != null) { | |
_onAccessingCalanderListener.onPanic(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment