Reputation: 322
I am trying to add calendar events to my custom account..
I have successfully created a calendar event using following code,
private static long addCalendar(Account account) throws RemoteException, OperationApplicationException {
Uri calenderUri = Calendars.CONTENT_URI.buildUpon().appendQueryParameter(Calendars.ACCOUNT_NAME, account.name).appendQueryParameter(
Calendars.ACCOUNT_TYPE, account.type).build();
Cursor c1 = mContentResolver.query(calenderUri, new String[] { BaseColumns._ID }, null, null, null);
if (c1.moveToNext()) {
return c1.getLong(0);
} else {
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(Calendars.CONTENT_URI.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(Calendars.ACCOUNT_NAME, account.name)
.appendQueryParameter(Calendars.ACCOUNT_TYPE, account.type)
.build()
);
builder.withValue(Calendars.ACCOUNT_NAME, account.name);
builder.withValue(Calendars.ACCOUNT_TYPE, account.type);
builder.withValue(Calendars.NAME, "com.example.karthi");
builder.withValue(Calendars.CALENDAR_DISPLAY_NAME, "com.example.karthi");
builder.withValue(Calendars.CALENDAR_COLOR, 0xD51007);
builder.withValue(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_READ);
builder.withValue(Calendars.OWNER_ACCOUNT, account.name);
builder.withValue(Calendars.VISIBLE, 1);
builder.withValue(Calendars.SYNC_EVENTS, 1);
operationList.add(builder.build());
try {
mContentResolver.applyBatch(CalendarContract.AUTHORITY, operationList);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
return addCalendar(account);
}
}
@SuppressLint("SimpleDateFormat")
public static void addEvent(Long id,Account account,String sid,String eventName,String s_date,String duration,String responsible) throws RemoteException, OperationApplicationException, ParseException{
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
Date begin_date = format.parse(String.valueOf(s_date));
long beginDate = begin_date.getTime();
long endDate = beginDate + (Double.valueOf(duration).intValue() * HOUR);
//Insert a new event
Log.d("add event"+calendarID,"area");
Log.d("event area","calendar id="+id);
TimeZone timeZone = TimeZone.getDefault();
ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(Events.CONTENT_URI.buildUpon()
.appendQueryParameter(Calendars.ACCOUNT_NAME, account.name)
.appendQueryParameter(Calendars.ACCOUNT_TYPE, account.type)
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.build()
);
builder.withValue(CalendarContract.Events.CALENDAR_ID, id);
builder.withValue(CalendarContract.Events.DTSTART, beginDate);
builder.withValue(CalendarContract.Events.DTEND, endDate);
builder.withValue(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
builder.withValue(CalendarContract.Events.TITLE, eventName);
operationList.add(builder.build());
mContentResolver.applyBatch(CalendarContract.AUTHORITY, operationList);
ContentProviderResult[] res = mContentResolver.applyBatch(CalendarContract.AUTHORITY, operationList);
Uri myContactUri = res[0].uri;
int lastSlash = myContactUri.toString().lastIndexOf("/");
int length = myContactUri.toString().length();
String event_id =(String) myContactUri.toString().subSequence(lastSlash+1, length);
s_date=String.valueOf(beginDate);
String e_date=String.valueOf(endDate);
DBAdapter.addEventsData(sid, eventName, s_date, e_date, responsible, event_id);
}
My account show in settings -> Calendars to display area... above code also create an event for my custom account.. This code work fine.
But I want to add a event in android emulator it give following error,
I also try Add account option. If I press Add account it allow to create my custom account.
and again try to create an event from android emulator. again it show No calendars message.. any help or commands welcome...
Note: I don`t want programatically add events,It already created from above code
Upvotes: 0
Views: 1070
Reputation: 322
Finally I found the solution. Its my mistake. I give access level only READ.
builder.withValue(Calendars.CALENDAR_ACCESS_LEVEL,Calendars.CAL_ACCESS_READ);
after change this line to,
builder.withValue(Calendars.CALENDAR_ACCESS_LEVEL,Calendars.CAL_ACCESS_OWNER);
Now its working fine.
Upvotes: 1