经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » HTML5 » 查看文章
H5+ 操作手机日历
来源:cnblogs  作者:掬水捧月可会不可言  时间:2020/11/9 16:02:05  对本文有异议

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

添加日历事件数据

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

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

  1. /*
  2. * cs 2020-9-28
  3. * 新增日历
  4. */
  5. function addEvent(Obj) {
  6. getCalendarJurisdiction(function() {
  7. // 查询是否有日历账号
  8. var calendarAccountList = plus.android.invoke(main.getContentResolver(), 'query', Uri.parse(calanderURL), null,
  9. null, null);
  10. var count = plus.android.invoke(calendarAccountList, "getCount");
  11. var tampArr = []
  12. while (plus.android.invoke(calendarAccountList, 'moveToNext')) {
  13. let tampObj = {}
  14. var keyLen = plus.android.invoke(calendarAccountList, 'getColumnCount');
  15. for (var i = 0; i < keyLen; i++) {
  16. var tampKey = plus.android.invoke(calendarAccountList, 'getColumnName', i);
  17. var tampVal = plus.android.invoke(calendarAccountList, 'getString', plus.android.invoke(calendarAccountList,
  18. 'getColumnIndex',
  19. tampKey));
  20. tampObj[tampKey] = tampVal;
  21. }
  22. tampArr.push(tampObj);
  23. }
  24. console.log(tampArr);
  25. // 查询是否有固定账号
  26. let isHasAccout = false;
  27. let accout_id = null;
  28. tampArr.forEach(item => {
  29. if (item.name == 'tansuoshengya') {
  30. accout_id = item._id;
  31. isHasAccout = true;
  32. }
  33. })
  34. // 如果有账号直接写入日历
  35. if (isHasAccout) {
  36. Obj.eventObj.calendar_id = accout_id;
  37. var eventID = addEvent_tabel(Obj.eventObj)
  38. } else {
  39. // 添加账号
  40. accout_id = initCalendars();
  41. Obj.eventObj.calendar_id = accout_id;
  42. var eventID = addEvent_tabel(Obj.eventObj)
  43. }
  44. if (Obj.success && typeof Obj.success == "function") {
  45. Obj.success({
  46. code: 200,
  47. data: eventID,
  48. msg: "获取成功"
  49. });
  50. }
  51. }, function() {
  52. if (Obj.error && typeof Obj.error == "function") {
  53. Obj.error({
  54. code: 404,
  55. msg: "添加失败"
  56. });
  57. }
  58. })
  59. }
  60. //添加账户
  61. function initCalendars() {
  62. var TimeZone = plus.android.importClass("java.util.TimeZone");
  63. var timeZone = TimeZone.getDefault();
  64. var ContentValues = plus.android.importClass("android.content.ContentValues");
  65. var value = new ContentValues();
  66. var Calendars = plus.android.importClass("android.provider.CalendarContract.Calendars");
  67. value.put("name", "tansuoshengya");
  68. value.put("account_name", "18071466586@163.com");
  69. value.put("account_type", "com.android.exchange");
  70. value.put("calendar_displayName", "全科提分能力");
  71. value.put("visible", 1);
  72. value.put("calendar_color", "-9206951");
  73. value.put("calendar_access_level", "700");
  74. value.put("sync_events", 1);
  75. value.put("calendar_timezone", plus.android.invoke(timeZone, "getID"));
  76. value.put("ownerAccount", "18071466586@163.com");
  77. value.put("canOrganizerRespond", 0);
  78. var Uri = plus.android.importClass("android.net.Uri");
  79. var calendarUri = Uri.parse("content://com.android.calendar/calendars");
  80. var buildUpon = plus.android.invoke(calendarUri, "buildUpon");
  81. var CalendarContract = plus.android.importClass("android.provider.CalendarContract");
  82. plus.android.invoke(buildUpon, "appendQueryParameter", CalendarContract.CALLER_IS_SYNCADAPTER, "true");
  83. plus.android.invoke(buildUpon, "appendQueryParameter", "account_name", "18071466586@163.com");
  84. plus.android.invoke(buildUpon, "appendQueryParameter", "account_type", "com.android.exchange");
  85. calendarUri = plus.android.invoke(buildUpon, "build");
  86. var newAccoutList = plus.android.invoke(plus.android.runtimeMainActivity().getContentResolver(), "insert", calendarUri,
  87. value);
  88. var newAccout_id;
  89. newAccout_id = plus.android.invoke(plus.android.invoke(newAccoutList, "getPathSegments"), "get", 1);
  90. return newAccout_id;
  91. }
  92. /*
  93. * 添加日历到表格
  94. * cs 2020-9-28
  95. * rrule 规则 FREQ=WEEKLY;WKST=SU;BYDAY=MO,TU,WE,TH,FR 每周的1 2 3 4 5
  96. * rrule='FREQ=DAILY;INTERVAL=5;WKST=SU' 每5天提醒
  97. * 每8天提醒一次,截止到20180808
  98. * rrule='FREQ=DAILY;UNTIL=20180808T093000;INTERVAL=8;WKST=SU'
  99. * dtstart 开始时间
  100. * eventTimezone 时区
  101. * dtend 结束时间 重复事件可以不用填
  102. */
  103. function addEvent_tabel(Obj) {
  104. var TimeZone = plus.android.importClass("java.util.TimeZone");
  105. var timeZone = TimeZone.getDefault();
  106. var ContentValues = plus.android.importClass("android.content.ContentValues");
  107. var values = new ContentValues();
  108. values.put("calendar_id", Obj.calendar_id);
  109. values.put("title", Obj.title);
  110. values.put("description", Obj.description);
  111. values.put("dtstart", Obj.dtstart);
  112. values.put("dtend", Obj.dtstart);
  113. values.put("eventTimezone", plus.android.invoke(timeZone, "getID"));
  114. values.put("rrule", Obj.rrule);
  115. values.put("hasAlarm", 1); //是否闹钟提醒 默认提醒 因为大部分手机未实现此功能 故未实现
  116. var newEvent = plus.android.invoke(main.getContentResolver(), 'insert', Uri.parse(calanderEventURL), values);
  117. console.log(newEvent);
  118. var id = plus.android.invoke(newEvent, 'getLastPathSegment');
  119. console.log(id);
  120. var remindersObj = new ContentValues();
  121. remindersObj.put('event_id', id);
  122. remindersObj.put('minutes', '5');
  123. remindersObj.put('method', '1');
  124. plus.android.invoke(main.getContentResolver(), 'insert', Uri.parse(calanderRemiderURL), remindersObj);
  125. console.log('设置提醒成功');
  126. return id;
  127. }

查询日历数据

  1. /*
  2. * cs 2020-9-26
  3. * 查询日历
  4. */
  5. function queryEvent(Obj) {
  6. // 首先获取权限
  7. getCalendarJurisdiction(function() {
  8. var userCursor = plus.android.invoke(main.getContentResolver(), "query", Uri.parse(calanderEventURL), null, null,
  9. null, null);
  10. var count = plus.android.invoke(userCursor, "getCount");
  11. var tampArr = []
  12. while (plus.android.invoke(userCursor, 'moveToNext')) {
  13. let tampObj = {}
  14. var keyLen = plus.android.invoke(userCursor, 'getColumnCount');
  15. for (var i = 0; i < keyLen; i++) {
  16. var tampKey = plus.android.invoke(userCursor, 'getColumnName', i);
  17. var tampVal = plus.android.invoke(userCursor, 'getString', plus.android.invoke(userCursor, 'getColumnIndex',
  18. tampKey));
  19. tampObj[tampKey] = tampVal;
  20. }
  21. tampArr.push(tampObj);
  22. console.log(tampObj)
  23. }
  24. console.log(typeof tampArr);
  25. if (Obj.success && typeof Obj.success == "function") {
  26. Obj.success({
  27. code: 200,
  28. data: tampArr,
  29. msg: "获取成功"
  30. });
  31. }
  32. }, function() {
  33. if (Obj.error && typeof Obj.error == "function") {
  34. Obj.error({
  35. code: 404,
  36. msg: "获取失败"
  37. });
  38. }
  39. })
  40. }

删除日历事件

  1. /*
  2. * cs 2020-9-28
  3. * 删除日历
  4. * event_id 事件的ID
  5. * success 成功回调
  6. * error 失败回调
  7. */
  8. function deleteEvent(Obj) {
  9. getCalendarJurisdiction(function() {
  10. console.log(Obj.event_id);
  11. var deleteUri = ContentUris.withAppendedId(Uri.parse(calanderEventURL), Obj.event_id);
  12. console.log(deleteUri);
  13. var rows = plus.android.invoke(main.getContentResolver(), "delete", deleteUri, null, null);
  14. if (Obj.success && typeof Obj.success == "function") {
  15. Obj.success({
  16. code: 200,
  17. data: rows,
  18. msg: "删除成功"
  19. });
  20. }
  21. }, function() {
  22. if (Obj.error && typeof Obj.error == "function") {
  23. Obj.error({
  24. code: 404,
  25. msg: "删除失败"
  26. });
  27. }
  28. })
  29. }

修改日历事件

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

完整代码

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

 

原文链接:http://www.cnblogs.com/csdcs/p/13754214.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号