I read a previous question Here, but it didn't quite answer mine. I am trying to do something as simple as pressing a button and entering an event, but program keeps on crashing. My code is:
package com.example.events;
import java.util.Calendar;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.provider.CalendarContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.example.events.R;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Perform action on click
addCalendarEvent();
}
});
}
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);
}
}
My AndroidManifest.xml looks like this
<?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>
So, basically what I am trying to do is press that button and add an event to the calendar without prompting the user to enter any details. Any help would be appreciated. This is the stack trace for errors:
08-29 18:14:44.108 13218-13218/com.example.events E/dalvikvm﹕ >>>>> Normal User
08-29 18:14:44.118 13218-13218/com.example.events E/dalvikvm﹕ >>>>> com.example.events [ userId:0 | appId:10255 ]
08-29 18:14:52.106 13218-13218/com.example.events E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.events, PID: 13218
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.calendar.CalendarProvider2 from ProcessRecord{44f66d78 13218:com.example.events/u0a255} (pid=13218, uid=10255) requires android.permission.READ_CALENDAR or android.permission.WRITE_CALENDAR
at android.os.Parcel.readException(Parcel.java:1465)
at android.os.Parcel.readException(Parcel.java:1419)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3299)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:4843)
at android.app.ContextImpl$ApplicationContentResolver.acquireProvider(ContextImpl.java:2718)
at android.content.ContentResolver.acquireProvider(ContentResolver.java:1390)
at android.content.ContentResolver.insert(ContentResolver.java:1196)
at com.example.events.MainActivity.addCalendarEvent(MainActivity.java:60)
at com.example.events.MainActivity$1.onClick(MainActivity.java:33)
at android.view.View.performClick(View.java:4753)
at android.view.View$PerformClick.run(View.java:19562)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)