2

I know, I know, in my android manifest I already declared the permissions! But perhaps at the wrong place?

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
//Lots of activities here
</application>

Yet, it's still giving me an error saying permission denied, you need to get either android.permission.READ_CALENDAR or WRITE_CALENDAR. I'm trying to insert an event into the default calendar. The following is my code:

Calendar begin = Calendar.getInstance();
            begin.set(dp.getYear(), dp.getMonth(), dp.getDayOfMonth(), tp.getHour(), tp.getMinute());
            long beginMillis = begin.getTimeInMillis();
            long endMillis = beginMillis + 1800000;
            String eventUriString = "content://com.android.calendar/events";
            ContentResolver cr = getContentResolver();
            ContentValues values = new ContentValues();
            values.put(Events.DTSTART, beginMillis);
            values.put(Events.DTEND, endMillis);
            values.put(Events.TITLE, "Flashcards Review: " + deckName);
            values.put(Events.CALENDAR_ID, 1);
            values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
            Uri eventUri = cr.insert(Uri.parse(eventUriString), values);

I couldn't really find a good tutorial so I don't know if this code even makes sense... Sorry! Please help!

tinzy
  • 21
  • 3

1 Answers1

2

For Calender

Calendar cal = Calendar.getInstance(); 
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,cal.getTimeInMillis()); 
intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,cal.getTimeInMillis()+60*60*1000);
intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true);
intent.putExtra(Events.TITLE, "Event title here");
intent.putExtra(Events.DESCRIPTION, "This is a sample description");
intent.putExtra(Events.EVENT_LOCATION, "Event Address");
intent.putExtra(Events.RRULE, "FREQ=YEARLY");
startActivity(intent);

and In Manifest.xml put uses permission as written below

<!-- Required to add events to Calendar -->
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

Hope this code will help you :)

Jenisha Makadiya
  • 832
  • 5
  • 17