我想创建日历。这是工作。但是如果安卓版本+4.0。这行不通。我知道我需要使用内容提供者,但我不知道如何。我希望该日历适用于所有版本的 Android。我该怎么做?
private void AddCalendar(int years,int months,int days,int hours,int minutes){
try{
// get calendar
Calendar cal = Calendar.getInstance();
Uri EVENTS_URI = Uri.parse("content://com.android.calendar/events");
ContentResolver cr = getContentResolver();
Calendar start = Calendar.getInstance();
start.set(years, months, days, hours, minutes, 0);
Calendar end = Calendar.getInstance();
end.set(years, months, days, hours + 1, minutes, 0);
long startTime = start.getTimeInMillis();
long endTime = end.getTimeInMillis();
// event insert
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", editText1.getText().toString());
values.put("allDay", 0);
values.put("dtstart", startTime);
values.put("dtend", endTime);
values.put("description", editText2.getText().toString());
values.put("visibility", 0);
values.put("hasAlarm", 1);
Uri event = cr.insert(EVENTS_URI, values);
// reminder insert
Uri REMINDERS_URI = Uri.parse("content://com.android.calendar/reminders");
values = new ContentValues();
values.put( "event_id", Long.parseLong(event.getLastPathSegment()));
values.put( "method", 1 );
values.put( "minutes", 10 );
cr.insert( REMINDERS_URI, values );
}catch (Exception e){
Toast.makeText(getApplicationContext(),
R.string.calendar, Toast.LENGTH_LONG).show();
}
}