Android开发修改本地日程数据demo 需要修改的数据有提醒时间,描述 title 开始时间和结束时间
时间: 2023-11-14 10:28:27 浏览: 275
好的,以下是一个修改日历事件提醒时间、描述、开始时间和结束时间的示例代码:
```java
// 获取日历事件的 ContentResolver 对象
ContentResolver cr = getContentResolver();
// 设置更新日历事件的 ContentProvider URI
Uri uri = CalendarContract.Events.CONTENT_URI;
// 设置更新日历事件的 where 子句
String selection = CalendarContract.Events._ID + " = ?";
// 设置更新日历事件的 where 子句参数
String[] selectionArgs = new String[] { String.valueOf(eventId) };
// 获取需要更新的日历事件
Cursor eventCursor = cr.query(uri, null, selection, selectionArgs, null);
if (eventCursor.moveToFirst()) {
// 获取需要更新的日历事件的各个字段
int id = eventCursor.getInt(eventCursor.getColumnIndex(CalendarContract.Events._ID));
String title = eventCursor.getString(eventCursor.getColumnIndex(CalendarContract.Events.TITLE));
String description = eventCursor.getString(eventCursor.getColumnIndex(CalendarContract.Events.DESCRIPTION));
long start = eventCursor.getLong(eventCursor.getColumnIndex(CalendarContract.Events.DTSTART));
long end = eventCursor.getLong(eventCursor.getColumnIndex(CalendarContract.Events.DTEND));
String timezone = eventCursor.getString(eventCursor.getColumnIndex(CalendarContract.Events.EVENT_TIMEZONE));
int hasAlarm = eventCursor.getInt(eventCursor.getColumnIndex(CalendarContract.Events.HAS_ALARM));
int calendarId = eventCursor.getInt(eventCursor.getColumnIndex(CalendarContract.Events.CALENDAR_ID));
// 进行修改操作
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.TITLE, "新的标题");
values.put(CalendarContract.Events.DESCRIPTION, "新的描述");
values.put(CalendarContract.Events.DTSTART, System.currentTimeMillis() + 1000 * 60 * 60);
values.put(CalendarContract.Events.DTEND, System.currentTimeMillis() + 1000 * 60 * 60 * 2);
values.put(CalendarContract.Events.EVENT_TIMEZONE, timezone);
values.put(CalendarContract.Events.HAS_ALARM, 1);
values.put(CalendarContract.Events.CALENDAR_ID, calendarId);
// 更新提醒时间
if (hasAlarm > 0) {
String reminderSelection = CalendarContract.Reminders.EVENT_ID + " = ?";
String[] reminderSelectionArgs = new String[] { String.valueOf(id) };
ContentValues reminderValues = new ContentValues();
reminderValues.put(CalendarContract.Reminders.MINUTES, 30);
int rows = cr.update(CalendarContract.Reminders.CONTENT_URI, reminderValues, reminderSelection, reminderSelectionArgs);
if (rows > 0) {
Log.d(TAG, "更新提醒时间成功");
} else {
Log.d(TAG, "更新提醒时间失败");
}
} else {
// 添加提醒时间
ContentValues reminderValues = new ContentValues();
reminderValues.put(CalendarContract.Reminders.MINUTES, 30);
reminderValues.put(CalendarContract.Reminders.EVENT_ID, id);
reminderValues.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
Uri reminderUri = cr.insert(CalendarContract.Reminders.CONTENT_URI, reminderValues);
if (reminderUri != null) {
Log.d(TAG, "添加提醒时间成功");
} else {
Log.d(TAG, "添加提醒时间失败");
}
}
// 更新日历事件
int rows = cr.update(uri, values, selection, selectionArgs);
// 输出修改结果
if (rows > 0) {
Log.d(TAG, "修改成功");
} else {
Log.d(TAG, "修改失败");
}
}
// 关闭游标
eventCursor.close();
```
在这个示例中,我们首先获取了日历事件的 ContentResolver 对象,然后设置了需要更新的 ContentProvider URI 和 where 子句,接着查询需要更新的日历事件并获取各个字段的值,然后进行修改操作并输出修改结果,最后关闭游标。需要注意的是,修改日历事件需要获取日历权限,并且更新提醒时间需要先判断是否已经存在提醒时间,如果不存在需要先添加提醒时间。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)