参考资料
Events
api讲解
添加权限
在config.json添加权限代码如下
"reqPermissions": [
{
"name": "ohos.permission.READ_CALENDAR"
},
{
"name": "ohos.permission.WRITE_CALENDAR"
},
{
"name": "ohos.permission.PUBLISH_AGENT_REMINDER"
在java代码动态注册权限
public class MainAbility extends Ability {
@Override
public void onStart(Intent intent) {
String[] permissions = {
"ohos.permission.READ_CALENDAR",
"ohos.permission.WRITE_CALENDAR",
"ohos.permission.PUBLISH_AGENT_REMINDER"
};
requestPermissionsFromUser(permissions, 0);
super.onStart(intent);
super.setMainRoute(MainAbilitySlice.class.getName());
}
}
添加日历事件
void insert(Context context) {
try {
EventsHelper = CalendarDataHelper.creator(context, Events.class);
//新增日历事件
ValuesBucket valuesBucket = new ValuesBucket();
valuesBucket.putInteger(EventsColumns.ACC_ID, 1);
valuesBucket.putNull(EventsColumns.DURATION);
valuesBucket.putString(EventsColumns.START_TIMEZONE, TimeZone.getDefault().getID());
valuesBucket.putString(EventsColumns.EVENT_POSITION, "上海");
valuesBucket.putString(EventsColumns.TITLE, "标题");
valuesBucket.putString(EventsColumns.DESCRIPTION, "描述");
valuesBucket.putLong(EventsColumns.EVENT_START_TIME, System.currentTimeMillis());
valuesBucket.putLong(EventsColumns.EVENT_END_TIME, System.currentTimeMillis()+8*1000*60*60);
boolean isok= EventsHelper.insert(valuesBucket);
System.err.println(isok?"####===>添加成功":"#####===>>添加失败");
} catch
修改日历事件
String PREDICTOR_TEMPLATE = "(%s)";
String sq1 = String.format(
Locale.ENGLISH,
PREDICTOR_TEMPLATE,
"calendar_id =" + 1);
DataAbilityPredicates predicates = new DataAbilityPredicates(sq1);
EventsHelper = CalendarDataHelper.creator(context, Events.class);
//新增日历事件
ValuesBucket valuesBucket = new ValuesBucket();
valuesBucket.putInteger(EventsColumns.ACC_ID, 1);
valuesBucket.putString(EventsColumns.TITLE, "修改标题");
//修改日历事件
boolean isupdate = EventsHelper.update(valuesBucket, predicates);
if (isupdate) {
System.err.println("#######====>修改成功");
} else {
System.err.println("#######====>修改失败");
}
查询日历事件
String PREDICTOR_TEMPLATE = "(%s)";
String sq1 = String.format(
Locale.ENGLISH,
PREDICTOR_TEMPLATE,
"calendar_id =" + 1);
DataAbilityPredicates predicates = new DataAbilityPredicates(sq1);
EventsHelper = CalendarDataHelper.creator(context, Events.class);
//查询日历事件
String[] eventColumns = new String[]{
Events.ID,
EventsColumns.TITLE,
EventsColumns.DESCRIPTION,
EventsColumns.EVENT_START_TIME,
EventsColumns.EVENT_END_TIME,
};
CalendarCollection calendarCollection = null;
calendarCollection = EventsHelper.query(predicates, eventColumns);
int id=-1;
while (calendarCollection != null && calendarCollection.hasNext()) {
Events resultEntity = (Events) calendarCollection.next()
.filter(entity -> entity instanceof Events)
.orElse(null);
System.err.println("####====>>"+resultEntity.getTitle() + "=" + resultEntity.getDescription()+"="+resultEntity.getId());
id=resultEntity.getId();
// Optional.ofNullable(calendarCollection).ifPresent(CalendarCollection::close);
删除日历事件
new DataAbilityPredicates("title like '%修改标题%'");
int flag = EventsHelper.delete(predicates);
System.err.println(flag > 0 ? "####===》删除成功" : "####===》删除失败");
添加日历提醒
new ValuesBucket();
reminderBucket.putInteger(RemindersColumns.EVENT_ID, id);
reminderBucket.putInteger(RemindersColumns.REMIND_TYPE, RemindersColumns.TYPE_ALARM);
reminderBucket.putInteger(RemindersColumns.REMIND_MINUTES, 1);
reminderBucket.putInteger("method",1);
boolean isok= false;
try {
isok = RemindersHelper.insert(reminderBucket);
} catch (DataAbilityRemoteException e) {
e.printStackTrace();
}
System.err.println(isok?"###===>设置成功"+id:"#####====>>设置失败"+id);
运行效果
全部代码
xml布局
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="vertical">
<Button
ohos:height="80vp"
ohos:width="match_parent"
ohos:text="添加事件"
ohos:text_size="20fp"
ohos:id="$+id:insertEvent"/>
<Button
ohos:height="80vp"
ohos:width="match_parent"
ohos:text="修改事件"
ohos:background_element="#ed6262"
ohos:text_size="20fp"
ohos:id="$+id:UpdateEvent"/>
<Button
ohos:height="80vp"
ohos:width="match_parent"
ohos:text="查询事件"
ohos:text_size="20fp"
ohos:id="$+id:selectEvent"/>
<Button
ohos:height="80vp"
ohos:width="match_parent"
ohos:text="设置提醒"
ohos:background_element="#ed6262"
ohos:text_size="20fp"
ohos:id="$+id:setReminders"/>
<Button
ohos:height="80vp"
ohos:width="match_parent"
ohos:text="删除"
ohos:text_size="20fp"
ohos:id="$+id:deleteEvent"/>
</DirectionalLayout>
java代码
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.data.rdb.ValuesBucket;
import ohos.sysappcomponents.calendar.CalendarCollection;
import ohos.sysappcomponents.calendar.CalendarDataHelper;
import ohos.sysappcomponents.calendar.column.EventsColumns;
import ohos.sysappcomponents.calendar.column.RemindersColumns;
import ohos.sysappcomponents.calendar.entity.Events;
import ohos.sysappcomponents.calendar.entity.Reminders;
import java.util.Locale;
import java.util.TimeZone;
public class MainAbilitySlice extends AbilitySlice
private CalendarDataHelper EventsHelper = CalendarDataHelper.creator(this, Events.class);
private int id;
CalendarDataHelper RemindersHelper = CalendarDataHelper.creator(this, Reminders.class);
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
findComponentById(ResourceTable.Id_insertEvent).setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
try {
//新增日历事件
ValuesBucket valuesBucket = new ValuesBucket();
valuesBucket.putInteger(EventsColumns.ACC_ID, 1);
valuesBucket.putNull(EventsColumns.DURATION);
valuesBucket.putString(EventsColumns.START_TIMEZONE, TimeZone.getDefault().getID());
valuesBucket.putString(EventsColumns.EVENT_POSITION, "上海");
valuesBucket.putString(EventsColumns.TITLE, "标题");
valuesBucket.putString(EventsColumns.DESCRIPTION, "描述");
valuesBucket.putLong(EventsColumns.EVENT_START_TIME, System.currentTimeMillis());
valuesBucket.putLong(EventsColumns.EVENT_END_TIME, System.currentTimeMillis()+8*1000*60*60);
boolean isok= EventsHelper.insert(valuesBucket);
System.err.println(isok?"####===>添加成功":"#####===>>添加失败");
} catch (DataAbilityRemoteException e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
});
findComponentById(ResourceTable.Id_UpdateEvent).setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
try {
String PREDICTOR_TEMPLATE = "(%s)";
String sq1 = String.format(
Locale.ENGLISH,
PREDICTOR_TEMPLATE,
"calendar_id =" + 1);
DataAbilityPredicates predicates = new DataAbilityPredicates(sq1);
//新增日历事件
ValuesBucket valuesBucket = new ValuesBucket();
valuesBucket.putInteger(EventsColumns.ACC_ID, 1);
valuesBucket.putString(EventsColumns.TITLE, "修改标题");
//修改日历事件
boolean isupdate = EventsHelper.update(valuesBucket, predicates);
if (isupdate) {
System.err.println("#######====>修改成功");
} else {
System.err.println("#######====>修改失败");
}
}catch (Exception e){
e.printStackTrace();
System.err.println("######====>"+e.getMessage());
}
}
});
findComponentById(ResourceTable.Id_selectEvent).setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
try {
String PREDICTOR_TEMPLATE = "(%s)";
String sq1 = String.format(
Locale.ENGLISH,
PREDICTOR_TEMPLATE,
"calendar_id =" + 1);
DataAbilityPredicates predicates = new DataAbilityPredicates(sq1);
//新增日历事件
//查询日历事件
String[] eventColumns = new String[]{
Events.ID,
EventsColumns.TITLE,
EventsColumns.DESCRIPTION,
EventsColumns.EVENT_START_TIME,
EventsColumns.EVENT_END_TIME,
};
CalendarCollection calendarCollection = null;
calendarCollection = EventsHelper.query(predicates, eventColumns);
while (calendarCollection != null && calendarCollection.hasNext()) {
Events resultEntity = (Events) calendarCollection.next()
.filter(entity -> entity instanceof Events)
.orElse(null);
System.err.println("####====>>"+resultEntity.getTitle() + "=" + resultEntity.getDescription()+"="+resultEntity.getId());
id=resultEntity.getId();
// Optional.ofNullable(calendarCollection).ifPresent(CalendarCollection::close);
}
} catch (DataAbilityRemoteException e) {
e.printStackTrace();
}
}
});
findComponentById(ResourceTable.Id_deleteEvent).setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
try {
DataAbilityPredicates predicates = new DataAbilityPredicates("title like '%修改标题%'");
int flag = EventsHelper.delete(predicates);
System.err.println(flag > 0 ? "####===》删除成功" : "####===》删除失败");
} catch (Exception e) {
e.printStackTrace();
}
}
});
findComponentById(ResourceTable.Id_setReminders).setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
ValuesBucket reminderBucket = new ValuesBucket();
reminderBucket.putInteger(RemindersColumns.EVENT_ID, id);
reminderBucket.putInteger(RemindersColumns.REMIND_TYPE, RemindersColumns.TYPE_ALARM);
reminderBucket.putInteger(RemindersColumns.REMIND_MINUTES, 1);
reminderBucket.putInteger("method",1);
boolean isok= false;
try {
isok = RemindersHelper.insert(reminderBucket);
} catch (DataAbilityRemoteException e) {
e.printStackTrace();
}
System.err.println(isok?"###===>设置成功"+id:"#####====>>设置失败"+id);
}
});
}
}
运行效果
欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh