I'm trying to add event to default android calendar whithout asking the user for a confirmation of saving the event. (so not with intent)
The following code has no compilation error or run time error. After clicking on the button, no error is shown and no event is added to the Android Calendar App.
I've already check the calendars authorisations in my manifest and check de min sdk version in the graddle (i work on android studio)
public void onAddEventClicked(View view) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
else if(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED) {
ContentResolver cr = getContentResolver();
Calendar beginTime = Calendar.getInstance();
beginTime.set(2018, Calendar.OCTOBER, 27, 13, 30);
Calendar endTime = Calendar.getInstance();
endTime.set(2018, Calendar.OCTOBER, 27, 17, 30);
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, beginTime.getTimeInMillis());
values.put(CalendarContract.Events.DTEND, endTime.getTimeInMillis());
values.put(CalendarContract.Events.TITLE, "doctor");
values.put(CalendarContract.Events.DESCRIPTION, "doctor appointment");
values.put(CalendarContract.Events.CALENDAR_ID, 0);
values.put(CalendarContract.Events.EVENT_TIMEZONE, "Europe/Paris");
values.put(CalendarContract.Events.EVENT_LOCATION, "Paris");
Uri EVENTS_URI = Uri.parse(CalendarContract.Events.CONTENT_URI.toString());
Uri uri = cr.insert(EVENTS_URI, values);
Button b = (Button) findViewById(R.id.addEventButton);
b.setText("Did it worked ?");
}
}