Event added through Calendar Provider is not showing up on the Android Calendar App
Asked Answered
C

4

3

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 ?");
    }
}
Continuant answered 27/10, 2017 at 0:24 Comment(1)
Did you figure out the issue ?? I was also having the same problem. It is because of the incorrect calendar ID. First we need to figure out the primary calendar ID of all the calendars present in the phone and then use that calendar ID to create the events. To get the primary calendar ID, you can follow the answer of Malwinder Singh. I also tried the same code and to my surprise i got two primary calendar IDs. I'm wondering which one to use.Unsubstantial
C
2

The default calendar id is 3, but you have 0 and that's probably why you don't see that being added to the Calendar.

From the Calendar Provider developer doc, it's hardcoding the calendar id to 3.

long calID = 3;
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 9, 14, 7, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 9, 14, 8, 45);
endMillis = endTime.getTimeInMillis();
...

ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, startMillis);
values.put(Events.DTEND, endMillis);
values.put(Events.TITLE, "Jazzercise");
values.put(Events.DESCRIPTION, "Group workout");
values.put(Events.CALENDAR_ID, calID);
values.put(Events.EVENT_TIMEZONE, "America/Los_Angeles");
Uri uri = cr.insert(Events.CONTENT_URI, values);

// get the event ID that is the last element in the Uri
long eventID = Long.parseLong(uri.getLastPathSegment());

To make sure 3 is the primary calendar id, you can refer this post for querying the primary/default calendar

Caucasian answered 15/12, 2017 at 19:29 Comment(0)
K
1

This will add an event to the users calendar.

Add permissions to the manifest:

 <uses-permission android:name="android.permission.READ_CALENDAR" />
 <uses-permission android:name="android.permission.WRITE_CALENDAR" />
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />


 <permission android:name="android.permission.READ_CALENDAR" />
 <permission android:name="android.permission.WRITE_CALENDAR" />
 <permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 <permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Add to oncreate:

if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) !=          PackageManager.PERMISSION_GRANTED)
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, 1);

Add to mainactivity: (View view) (MenuItem mi) use for the menu drawer.

public void onAddEventCal(MenuItem mi) {

    long calID = 3;
    ContentResolver cr = getContentResolver();
    Calendar beginTime = Calendar.getInstance();
    beginTime.set(2019, Calendar.FEBRUARY, 25, 3, 30);
    Calendar endTime = Calendar.getInstance();
    endTime.set(2019, Calendar.FEBRUARY, 25, 3, 30);
    ContentValues values = new ContentValues();
    values.put(CalendarContract.Events.DTSTART, beginTime.getTimeInMillis());
    values.put(CalendarContract.Events.DTEND, endTime.getTimeInMillis());
    values.put(CalendarContract.Events.TITLE, "Connect Treadmill app");
    values.put(CalendarContract.Events.DESCRIPTION, "Treadmill Data");
    values.put(CalendarContract.Events.CALENDAR_ID, calID);
    values.put(CalendarContract.Events.EVENT_TIMEZONE, "Europe/Paris");
    values.put(CalendarContract.Events.EVENT_LOCATION, "Paris");
    Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);


    Toast.makeText(this, "Calendar Event added", Toast.LENGTH_SHORT).show();

}
Kimmi answered 26/2, 2019 at 2:56 Comment(0)
S
0

I think the problem is in permission try using these code

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},MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);}else if(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED 
        && ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED){



}
Succinct answered 27/10, 2017 at 1:0 Comment(6)
I tried with just the number 1 instead of your "MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION" for test it because google says it should be a const int so... let's try because i have no idea where i can grab it. I'm still obtaining the same result by that way. I mean when i click on the button the text changes so, it's paasing trough all the code but still no new event in calendar I just put the version with your modification on te postContinuant
Do you check that there is a calendar with the calendar IdSuccinct
Just adding, when adding an event you must specify the calendar_id the vent belongs to. See this document for reference. To get the default calendar_id, check this related SO post. Hope this helps.O
Thanks for help guys but it still not working. @MR.Rebot : i don't understand the field calendar_id is waiting for an int but the way for get it in your "related SO post" return a string ?Continuant
I really need some help around here, do somebody have an idea ? I'm still not sure about hw to get the calendar_idContinuant
@El-jBlaack, You can follow Malwinder Singh's answer to get the primary calendar ID. I tried it and it is working.Unsubstantial
K
0

This permission request is what I used. You can modify to what permissions you want.

 onCreate

 // Ask for location && calendar permission if not already allowed
    Request_Permissions();




ManActivity
//Request permission
private void Request_Permissions()
{
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR, android.Manifest.permission.ACCESS_COARSE_LOCATION
    }, 1);
}
Kimmi answered 12/3, 2019 at 4:53 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.