如何在没有意图 Android 8 或 > 的情况下将多个事件添加到日历?
How add mutiple events to calendar without intent Android 8 or >?
我已经找到了关于此的问题,但就我而言,什么也没有发生。
Event added through Calendar Provider is not showing up on the Android Calendar App
我试着按照这个做,但显然我错过了一些东西,因为它不起作用
我想在日历中添加几个事件。
我已经尝试使用 1 但没有任何反应
我目前未满 android 10
public void addEventToCalendar(){
long calID = 3;
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2021, 10, 22, 15, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2021, 10, 22, 16, 45);
endMillis = endTime.getTimeInMillis();
ContentResolver cr = getActivity().getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.TITLE, "Hackathon");
values.put(CalendarContract.Events.DESCRIPTION, "do some code");
values.put(CalendarContract.Events.CALENDAR_ID, calID);
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
long eventID = Long.parseLong(uri.getLastPathSegment());
}
我只是把它放在 class 上,我正在使用一个 CSV 文件,我只是在其中添加我的不同事件,它适用于不同的设备 android 并在 Oreo、Pie 上测试和 Android 10
public class EventCalendar {
String startEvent;
String endEvent;
String title;
String description;
public String getStartEvent() {
return startEvent;
}
public void setStartEvent(String startEvent) {
this.startEvent = startEvent;
}
public String getEndEvent() {
return endEvent;
}
public void setEndEvent(String endEvent) {
this.endEvent = endEvent;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void addEventCalendar(Context myContext, InputStream is) {
List<EventCalendar> mesEvent = new ArrayList<>();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, StandardCharsets.UTF_8));
String line;
try {
reader.readLine();
while ((line = reader.readLine()) != null) {
Log.d("MyEventCalendar", "Line : " + line);
String[] tokens = line.split(",");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date startDate;
Date endDate;
startDate = sdf.parse(tokens[0]);
endDate = sdf.parse(tokens[1]);
EventCalendar myEventCalendar = new EventCalendar();
myEventCalendar.setTitle(tokens[2]);
if (tokens.length >= 4){
myEventCalendar.setDescription(tokens[3]);
}else {
myEventCalendar.setDescription(null);
}
myEventCalendar.setStartEvent(String.valueOf(startDate.getTime()));
myEventCalendar.setEndEvent(String.valueOf(endDate.getTime()));
mesEvent.add(myEventCalendar);
ContentResolver cr = Objects.requireNonNull(myContext.getContentResolver());
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, myEventCalendar.getStartEvent());
values.put(CalendarContract.Events.DTEND, myEventCalendar.getEndEvent());
values.put(CalendarContract.Events.TITLE, myEventCalendar.getTitle());
values.put(CalendarContract.Events.DESCRIPTION, myEventCalendar.getDescription());
values.put(CalendarContract.Events.CALENDAR_ID, 1);
values.put(CalendarContract.Events.EVENT_LOCATION,0);
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
}
} catch (IOException | ParseException e) {
Log.i("Tag", "Insert failed");
e.printStackTrace();
}
}
我已经找到了关于此的问题,但就我而言,什么也没有发生。 Event added through Calendar Provider is not showing up on the Android Calendar App 我试着按照这个做,但显然我错过了一些东西,因为它不起作用
我想在日历中添加几个事件。 我已经尝试使用 1 但没有任何反应
我目前未满 android 10
public void addEventToCalendar(){
long calID = 3;
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2021, 10, 22, 15, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2021, 10, 22, 16, 45);
endMillis = endTime.getTimeInMillis();
ContentResolver cr = getActivity().getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.TITLE, "Hackathon");
values.put(CalendarContract.Events.DESCRIPTION, "do some code");
values.put(CalendarContract.Events.CALENDAR_ID, calID);
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
long eventID = Long.parseLong(uri.getLastPathSegment());
}
我只是把它放在 class 上,我正在使用一个 CSV 文件,我只是在其中添加我的不同事件,它适用于不同的设备 android 并在 Oreo、Pie 上测试和 Android 10
public class EventCalendar {
String startEvent;
String endEvent;
String title;
String description;
public String getStartEvent() {
return startEvent;
}
public void setStartEvent(String startEvent) {
this.startEvent = startEvent;
}
public String getEndEvent() {
return endEvent;
}
public void setEndEvent(String endEvent) {
this.endEvent = endEvent;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void addEventCalendar(Context myContext, InputStream is) {
List<EventCalendar> mesEvent = new ArrayList<>();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, StandardCharsets.UTF_8));
String line;
try {
reader.readLine();
while ((line = reader.readLine()) != null) {
Log.d("MyEventCalendar", "Line : " + line);
String[] tokens = line.split(",");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date startDate;
Date endDate;
startDate = sdf.parse(tokens[0]);
endDate = sdf.parse(tokens[1]);
EventCalendar myEventCalendar = new EventCalendar();
myEventCalendar.setTitle(tokens[2]);
if (tokens.length >= 4){
myEventCalendar.setDescription(tokens[3]);
}else {
myEventCalendar.setDescription(null);
}
myEventCalendar.setStartEvent(String.valueOf(startDate.getTime()));
myEventCalendar.setEndEvent(String.valueOf(endDate.getTime()));
mesEvent.add(myEventCalendar);
ContentResolver cr = Objects.requireNonNull(myContext.getContentResolver());
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, myEventCalendar.getStartEvent());
values.put(CalendarContract.Events.DTEND, myEventCalendar.getEndEvent());
values.put(CalendarContract.Events.TITLE, myEventCalendar.getTitle());
values.put(CalendarContract.Events.DESCRIPTION, myEventCalendar.getDescription());
values.put(CalendarContract.Events.CALENDAR_ID, 1);
values.put(CalendarContract.Events.EVENT_LOCATION,0);
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
}
} catch (IOException | ParseException e) {
Log.i("Tag", "Insert failed");
e.printStackTrace();
}
}