有没有人知道如何在android中为usercreated日历添加事件.. 我是创建日历然后我只想添加事件到这个日历 我不知道在此日历中添加活动..... plz help
这个mainactivity定义它返回创建的日历的uri。 calender已成功创建,但如何使用以及如何将事件添加到日历
public class MainActivity extends AppCompatActivity
{
String Eventmanagement="eventmanagement";
String accountName="com.google";
protected Uri baseUri = Uri.parse("content://com.android.calendar/calendars");
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Uri uri=createCalendarWithName(MainActivity.this,Eventmanagement,accountName);
//System.out.println(uri);
}
public Uri createCalendarWithName(Context ctx, String name,String accountName) {
Uri target = createCompleteUri();
// target = target.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
// .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, accountName)
// .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google").build();
ContentValues values = new ContentValues();
values.put(CalendarContract.Calendars.ACCOUNT_NAME, accountName);
values.put(CalendarContract.Calendars._ID,11);
values.put(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
values.put(CalendarContract.Calendars.NAME, name);
values.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, name);
values.put(CalendarContract.Calendars.CALENDAR_COLOR, 0x00FF00);
values.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_ROOT);
values.put(CalendarContract.Calendars.OWNER_ACCOUNT, accountName);
values.put(CalendarContract.Calendars.VISIBLE, 1);
values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
values.put(CalendarContract.Calendars.CALENDAR_TIME_ZONE, "Europe/Rome");
values.put(CalendarContract.Calendars.CAN_PARTIALLY_UPDATE, 1);
// values.put(CalendarContract.Calendars.CAL_SYNC1, "https://www.google.com/calendar/feeds/" + accountName + "/private/full");
// values.put(CalendarContract.Calendars.CAL_SYNC2, "https://www.google.com/calendar/feeds/default/allcalendars/full/" + accountName);
// values.put(CalendarContract.Calendars.CAL_SYNC3, "https://www.google.com/calendar/feeds/default/allcalendars/full/" + accountName);
// values.put(CalendarContract.Calendars.CAL_SYNC4, 1);
// values.put(CalendarContract.Calendars.CAL_SYNC5, 0);
// values.put(CalendarContract.Calendars.CAL_SYNC8, System.currentTimeMillis());
Uri newCalendar = ctx.getContentResolver().insert(target, values);
System.out.println("created succesfully");
return newCalendar;
}
private Uri createCompleteUri()
{
Uri.Builder b =baseUri.buildUpon();
b.appendQueryParameter("caller_is_syncadapter", "true");
b.appendQueryParameter("account_name", "local");
b.appendQueryParameter("account_type", "LOCAL");
Uri calUri = b.build();
Log.d("completeUri", calUri.toString());
return calUri;
}
}
答案 0 :(得分:0)
1.检查documentation
2.然后看tutorial
...
long calId = getCalendarId();
if (calId == -1) {
// no calendar account; react meaningfully
return;
}
Calendar cal = new GregorianCalendar(2012, 11, 14);
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
long start = cal.getTimeInMillis();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, start);
values.put(Events.DTEND, start);
values.put(Events.RRULE,
"FREQ=DAILY;COUNT=20;BYDAY=MO,TU,WE,TH,FR;WKST=MO");
values.put(Events.TITLE, "Some title");
values.put(Events.EVENT_LOCATION, "Münster");
values.put(Events.CALENDAR_ID, calId);
values.put(Events.EVENT_TIMEZONE, "Europe/Berlin");
values.put(Events.DESCRIPTION,
"The agenda or some description of the event");
// reasonable defaults exist:
values.put(Events.ACCESS_LEVEL, Events.ACCESS_PRIVATE);
values.put(Events.SELF_ATTENDEE_STATUS,
Events.STATUS_CONFIRMED);
values.put(Events.ALL_DAY, 1);
values.put(Events.ORGANIZER, "some.mail@some.address.com");
values.put(Events.GUESTS_CAN_INVITE_OTHERS, 1);
values.put(Events.GUESTS_CAN_MODIFY, 1);
values.put(Events.AVAILABILITY, Events.AVAILABILITY_BUSY);
Uri uri =
getContentResolver().
insert(Events.CONTENT_URI, values);
long eventId = new Long(uri.getLastPathSegment());
...