H5+ 操作手机日历

  • A+
所属分类:Web前端
摘要

参考文章 https://ask.dcloud.net.cn/article/215  使用native.js操作安卓原生系统

参考文章 https://ask.dcloud.net.cn/article/215  使用native.js操作安卓原生系统

 https://blog.csdn.net/wenzhi20102321/article/details/80644833 在android中 操作日历

使用的

content://com.android.calendar/calendars  系统日历账号表

content://com.android.calendar/events 系统日历事件表

content://com.android.calendar/reminders 系统提现表

 

 获取系统日历权限

 1 /*  2  * cs 2020-9-26  3  * 获取日历权限  4  */  5 function getCalendarJurisdiction(sucFun, errFun) {  6     plus.android.requestPermissions(['android.permission.READ_CALENDAR', 'android.permission.WRITE_CALENDAR'], function(e) {  7         if (e.deniedAlways.length > 0) { //权限被永久拒绝  8             // 弹出提示框解释为何需要定位权限,引导用户打开设置页面开启  9             console.log('Always Denied!!! ' + e.deniedAlways.toString()); 10             if (errFun && typeof errFun == "function") { 11                 errFun({ 12                     msg: "权限被永久拒绝了" 13                 }) 14             } 15         } 16         if (e.deniedPresent.length > 0) { //权限被临时拒绝 17             if (errFun && typeof errFun == "function") { 18                 errFun({ 19                     msg: "权限被临时拒绝了" 20                 }) 21             } 22             // 弹出提示框解释为何需要定位权限,可再次调用plus.android.requestPermissions申请权限 23             console.log('Present Denied!!! ' + e.deniedPresent.toString()); 24         } 25         if (e.granted.length > 0) { //权限被允许 26             //调用依赖获取定位权限的代码 27             if (sucFun && typeof sucFun == 'function') { 28                 sucFun(); 29             } 30         } 31     }) 32 }

添加日历事件数据

逻辑说明, 第一步 获取操作日历权限,然后获取app对应的日历账号,如果不存在则调用方法创建日历账号,返回账号ID,即calendar_id

然后通过calendar_id插入日历事件,返回事件ID,根据事件ID 插入日历提醒,代码如下:

/*  * cs 2020-9-28  * 新增日历  */ function addEvent(Obj) {     getCalendarJurisdiction(function() {         // 查询是否有日历账号         var calendarAccountList = plus.android.invoke(main.getContentResolver(), 'query', Uri.parse(calanderURL), null,             null, null);          var count = plus.android.invoke(calendarAccountList, "getCount");         var tampArr = []         while (plus.android.invoke(calendarAccountList, 'moveToNext')) {             let tampObj = {}             var keyLen = plus.android.invoke(calendarAccountList, 'getColumnCount');             for (var i = 0; i < keyLen; i++) {                 var tampKey = plus.android.invoke(calendarAccountList, 'getColumnName', i);                 var tampVal = plus.android.invoke(calendarAccountList, 'getString', plus.android.invoke(calendarAccountList,                     'getColumnIndex',                     tampKey));                 tampObj[tampKey] = tampVal;             }             tampArr.push(tampObj);         }         console.log(tampArr);         // 查询是否有固定账号         let isHasAccout = false;         let accout_id = null;          tampArr.forEach(item => {             if (item.name == 'tansuoshengya') {                 accout_id = item._id;                 isHasAccout = true;             }         })          // 如果有账号直接写入日历         if (isHasAccout) {             Obj.eventObj.calendar_id = accout_id;             var eventID = addEvent_tabel(Obj.eventObj)          } else {             // 添加账号             accout_id = initCalendars();             Obj.eventObj.calendar_id = accout_id;              var eventID = addEvent_tabel(Obj.eventObj)         }          if (Obj.success && typeof Obj.success == "function") {             Obj.success({                 code: 200,                 data: eventID,                 msg: "获取成功"             });         }      }, function() {         if (Obj.error && typeof Obj.error == "function") {             Obj.error({                 code: 404,                 msg: "添加失败"             });         }     }) }   //添加账户   function initCalendars() {     var TimeZone = plus.android.importClass("java.util.TimeZone");     var timeZone = TimeZone.getDefault();     var ContentValues = plus.android.importClass("android.content.ContentValues");     var value = new ContentValues();     var Calendars = plus.android.importClass("android.provider.CalendarContract.Calendars");     value.put("name", "tansuoshengya");     value.put("account_name", "18071466586@163.com");     value.put("account_type", "com.android.exchange");     value.put("calendar_displayName", "全科提分能力");     value.put("visible", 1);     value.put("calendar_color", "-9206951");     value.put("calendar_access_level", "700");     value.put("sync_events", 1);     value.put("calendar_timezone", plus.android.invoke(timeZone, "getID"));     value.put("ownerAccount", "18071466586@163.com");     value.put("canOrganizerRespond", 0);     var Uri = plus.android.importClass("android.net.Uri");     var calendarUri = Uri.parse("content://com.android.calendar/calendars");     var buildUpon = plus.android.invoke(calendarUri, "buildUpon");     var CalendarContract = plus.android.importClass("android.provider.CalendarContract");     plus.android.invoke(buildUpon, "appendQueryParameter", CalendarContract.CALLER_IS_SYNCADAPTER, "true");     plus.android.invoke(buildUpon, "appendQueryParameter", "account_name", "18071466586@163.com");     plus.android.invoke(buildUpon, "appendQueryParameter", "account_type", "com.android.exchange");     calendarUri = plus.android.invoke(buildUpon, "build");     var newAccoutList = plus.android.invoke(plus.android.runtimeMainActivity().getContentResolver(), "insert", calendarUri,         value);      var newAccout_id;     newAccout_id = plus.android.invoke(plus.android.invoke(newAccoutList, "getPathSegments"), "get", 1);      return newAccout_id; }  /*  * 添加日历到表格  * cs 2020-9-28  * rrule 规则 FREQ=WEEKLY;WKST=SU;BYDAY=MO,TU,WE,TH,FR 每周的1 2 3 4 5  * rrule='FREQ=DAILY;INTERVAL=5;WKST=SU' 每5天提醒  * 每8天提醒一次,截止到20180808  * rrule='FREQ=DAILY;UNTIL=20180808T093000;INTERVAL=8;WKST=SU'  * dtstart 开始时间  * eventTimezone 时区  * dtend 结束时间 重复事件可以不用填  */ function addEvent_tabel(Obj) {     var TimeZone = plus.android.importClass("java.util.TimeZone");     var timeZone = TimeZone.getDefault();     var ContentValues = plus.android.importClass("android.content.ContentValues");     var values = new ContentValues();     values.put("calendar_id", Obj.calendar_id);     values.put("title", Obj.title);     values.put("description", Obj.description);     values.put("dtstart", Obj.dtstart);     values.put("dtend", Obj.dtstart);     values.put("eventTimezone", plus.android.invoke(timeZone, "getID"));     values.put("rrule", Obj.rrule);     values.put("hasAlarm", 1); //是否闹钟提醒 默认提醒 因为大部分手机未实现此功能 故未实现     var newEvent = plus.android.invoke(main.getContentResolver(), 'insert', Uri.parse(calanderEventURL), values);     console.log(newEvent);     var id = plus.android.invoke(newEvent, 'getLastPathSegment');     console.log(id);     var remindersObj = new ContentValues();     remindersObj.put('event_id', id);     remindersObj.put('minutes', '5');     remindersObj.put('method', '1');     plus.android.invoke(main.getContentResolver(), 'insert', Uri.parse(calanderRemiderURL), remindersObj);     console.log('设置提醒成功');     return id; }

查询日历数据

/*  * cs 2020-9-26  * 查询日历  */ function queryEvent(Obj) {     // 首先获取权限     getCalendarJurisdiction(function() {         var userCursor = plus.android.invoke(main.getContentResolver(), "query", Uri.parse(calanderEventURL), null, null,             null, null);          var count = plus.android.invoke(userCursor, "getCount");         var tampArr = []         while (plus.android.invoke(userCursor, 'moveToNext')) {             let tampObj = {}             var keyLen = plus.android.invoke(userCursor, 'getColumnCount');             for (var i = 0; i < keyLen; i++) {                 var tampKey = plus.android.invoke(userCursor, 'getColumnName', i);                 var tampVal = plus.android.invoke(userCursor, 'getString', plus.android.invoke(userCursor, 'getColumnIndex',                     tampKey));                 tampObj[tampKey] = tampVal;             }             tampArr.push(tampObj);             console.log(tampObj)         }         console.log(typeof tampArr);          if (Obj.success && typeof Obj.success == "function") {             Obj.success({                 code: 200,                 data: tampArr,                 msg: "获取成功"             });         }      }, function() {         if (Obj.error && typeof Obj.error == "function") {             Obj.error({                 code: 404,                 msg: "获取失败"             });         }     }) }

删除日历事件

/*  * cs 2020-9-28  * 删除日历  * event_id 事件的ID  * success 成功回调  * error 失败回调  */ function deleteEvent(Obj) {     getCalendarJurisdiction(function() {         console.log(Obj.event_id);         var deleteUri = ContentUris.withAppendedId(Uri.parse(calanderEventURL), Obj.event_id);         console.log(deleteUri);         var rows = plus.android.invoke(main.getContentResolver(), "delete", deleteUri, null, null);          if (Obj.success && typeof Obj.success == "function") {             Obj.success({                 code: 200,                 data: rows,                 msg: "删除成功"             });         }      }, function() {         if (Obj.error && typeof Obj.error == "function") {             Obj.error({                 code: 404,                 msg: "删除失败"             });         }     }) }

修改日历事件

 1 /*  2  * cs 2020-9-28  3  * 修改日历事件  4  * eventObj 参数必须要event_id  5  */  6 function updataEvent(Obj) {  7     getCalendarJurisdiction(function() {  8         if (!Obj.eventObj.event_id) {  9             if (Obj.error && typeof Obj.error == "function") { 10                 Obj.error({ 11                     code: 404, 12                     msg: "添加失败" 13                 }); 14             } 15         } 16  17         var values = new ContentValues(); 18         if (Obj.eventObj.title) { 19             values.put("title", Obj.eventObj.title) 20         } 21         if (Obj.eventObj.description) { 22             values.put("description", Obj.eventObj.description) 23         } 24         if (Obj.eventObj.dtstart) { 25             values.put("dtstart", Obj.eventObj.dtstart) 26         } 27         if (Obj.eventObj.dtstart) { 28             values.put("dtend", Obj.eventObj.dtstart) 29         } 30         if (Obj.eventObj.rrule) { 31             values.put("rrule", Obj.eventObj.rrule) 32         } 33         console.log(Obj.eventObj.event_id) 34         var updateUri = ContentUris.withAppendedId(Uri.parse(calanderEventURL), Obj.eventObj.event_id); 35         console.log(updateUri); 36         var rows = plus.android.invoke(main.getContentResolver(), "update", updateUri, values, null, null); 37         if (Obj.success && typeof Obj.success == "function") { 38             Obj.success({ 39                 code: 200, 40                 data: rows, 41                 msg: "修改成功" 42             }); 43         } 44  45     }, function() { 46         if (Obj.error && typeof Obj.error == "function") { 47             Obj.error({ 48                 code: 404, 49                 msg: "修改成功" 50             }); 51         } 52     }) 53 }

完整代码

https://blog-static.cnblogs.com/files/csdcs/calendar.js