1

I am using a button, and calling the following method inside of the onClick Event:

public void addCalendarEvent() {

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

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

After trying to debug it, I get the following exception:

java.lang.SecurityException: Permission Denial: opening provider com.android.providers.calendar.CalendarProvider2 from ProcessRecord{438ea3d0 30931:com.example.events/u0a255} (pid=30931, uid=10255) requires android.permission.READ_CALENDAR or android.permission.WRITE_CALENDAR

Although I have already added the user permissions to my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.events" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <uses-sdk android:minSdkVersion="14" />
    <uses-permission android:name="android.permission.WRITE_CALENDAR" />
    <uses-permission android:name="android.permission.READ_CALENDAR" />
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

Any help is appreciated. Thanks!

2 Answers2

1

Try this

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

  <application>

  </application>

  </manifest>
Shanmugasundharam
  • 2,082
  • 23
  • 32
0

You have to include the permissions outside the application tag, inside the manifest tag. That's like the standard I guess. But it doesn't work for me even if I put them outside. Maybe I've got some other problem coupled up, so I thought if it might work for you. Try it, don't worry if it doesn't. Because it doesn't work for me as well, and any help for me too would be appreciated

Sameer Achanta
  • 383
  • 4
  • 18
  • I asked the question again here http://stackoverflow.com/questions/32290820/how-to-add-an-event-to-a-calendar-silently-in-android-studio and the answer was just like what you said. Check my code and I hope it helps. – timon_the_destroyer Sep 11 '15 at 14:47