开发工具
HBuilder X 3.8.12.20230817
注意:开发工具尽量用最新的或较新的。太旧的版本在开发调试过程中可能会出现莫名其妙的问题。
1、需求分析
之前APP需要实现 日历提醒功能(功能相对的麻烦一点:需要日(每天)提醒、周提醒、按月提醒、按季度提醒,这几种提醒是不同的类型,可能会在同一天提醒。),软件使用的MUI框架及HBuilderX工具打包的,网上的资料也少。不过在官方论坛中还是找到了
2、代码实现
上面已经说明了,官方已经有相关的资料实现 日历提醒功能。
官方社区资料链接:mui(准确的说是html5+)Android原生日历提醒插入案例踩坑经历 - DCloud问答
分享一个Android原生日历提醒插入案例。。。求个iOS的??谢谢。。。。。 - DCloud问答
注意:操作安卓日历,需要开启日历权限、还需要账号(账号用下面代码中的即可)
代码:
(function($) {
var setcalendar = function() {
$.toast('功能加载中,请稍后', {
type: 'div',
duration: 1000
});
};
$.plusReady(function() {
var calanderURL = 'content://com.android.calendar/calendars',
ContentValues = plus.android.importClass("android.content.ContentValues"),
Uri = plus.android.importClass('android.net.Uri'),
Calendar = plus.android.importClass('java.util.Calendar'),
main = plus.android.runtimeMainActivity(),
userCursor = plus.android.invoke(main.getContentResolver(), 'query', Uri.parse(calanderURL), null, null, null, null),
userCursor_count = plus.android.invoke(userCursor, 'getCount'),
TimeZone = plus.android.importClass('java.util.TimeZone'),
TimeZone_str = plus.android.invoke(TimeZone.getDefault(), 'getID');
setcalendar = function(title, description, date_str) {
if(userCursor_count <= 0) {//如果没有日历账户
var account = new ContentValues(),
buildUpon = plus.android.invoke(Uri.parse(calanderURL), 'buildUpon'),
CalendarContract = plus.android.importClass('android.provider.CalendarContract');
plus.android.invoke(buildUpon, 'appendQueryParameter', CalendarContract.CALLER_IS_SYNCADAPTER, 'true');
plus.android.invoke(buildUpon, 'appendQueryParameter', 'account_name', 'someone@something.com');
plus.android.invoke(buildUpon, 'appendQueryParameter', 'account_type', 'com.android.exchange');
//设置账户信息
account.put('name', 'someone');
account.put('account_name', 'someone@something.com');
account.put('account_type', 'com.android.exchange');
account.put('calendar_displayName', 'someone_calendar');
account.put('visible', 1);
account.put('calendar_color', '-9206951');
account.put('calendar_access_level', '700');
account.put('sync_events', 1);
account.put('calendar_timezone', TimeZone_str);
account.put('ownerAccount', 'someone@something.com');
account.put('canOrganizerRespond', 0);
//保存账户信息
plus.android.invoke(main.getContentResolver(), 'insert', plus.android.invoke(buildUpon, 'build'), account);
//重新定义userCursor
userCursor = plus.android.invoke(main.getContentResolver(), 'query', Uri.parse(calanderURL), null, null, null, null);
//重新定义userCursor_count
userCursor_count++;
}
plus.android.invoke(userCursor, 'moveToLast');
var calId = plus.android.invoke(userCursor, 'getString', plus.android.invoke(userCursor, 'getColumnIndex', '_id')),
events = new ContentValues(),
mCalendar = Calendar.getInstance(),
date = date_str.split(/\s{1}|:|-/g);
plus.android.invoke(mCalendar, 'set', Calendar.YEAR, ~~date[0]);
plus.android.invoke(mCalendar, 'set', Calendar.MONTH, ((~~date[1]) - 1));
plus.android.invoke(mCalendar, 'set', Calendar.DATE, ~~date[2]);
plus.android.invoke(mCalendar, 'set', Calendar.HOUR_OF_DAY, ~~date[3]);
plus.android.invoke(mCalendar, 'set', Calendar.MINUTE, ~~date[4]);
var start = plus.android.invoke(plus.android.invoke(mCalendar, 'getTime'), 'getTime'),
end = plus.android.invoke(plus.android.invoke(mCalendar, 'getTime'), 'getTime');
//设置日历事件
events.put('title', title);
events.put('description', description);
events.put('calendar_id', calId);
events.put('dtstart', start);
events.put('dtend', end);
events.put('hasAlarm', 1);
events.put('eventTimezone', TimeZone_str);
var newEvent = plus.android.invoke(main.getContentResolver(), 'insert', Uri.parse('content://com.android.calendar/events'), events);
var id = plus.android.invoke(newEvent, 'getLastPathSegment');
var values = new ContentValues();
values.put('event_id', id);
values.put('minutes', '5');
values.put('method', '1');
plus.android.invoke(main.getContentResolver(), 'insert', Uri.parse('content://com.android.calendar/reminders'), values);
$.toast('设置提醒成功');
}
});
$.ready(function() {
$('.mui-content').on('tap', 'button.mui-btn', function() {
var picker = new $.DtPicker();
picker.show(function(rs) {
setcalendar('测试提醒标题', '测试提醒内容', rs.text);
picker.dispose();
});
});
});
})(mui);
如果没有特殊需求,以上代码即可完成简单的日历提醒功能。
3、代码优化
第一步已经说了,APP需要实现 日历提醒功能(功能相对的麻烦一点:需要日(每天)提醒、周提醒、按月提醒、按季度提醒,这几种提醒是不同的类型,可能会在同一天提醒。)
优化后的代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>巡检设置</title>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<link href="css/mui.min.css" rel="stylesheet" />
<link href="css/style.css" rel="stylesheet" />
<style>
html,
body {
background-color: #efeff4;
}
.mui-bar~.mui-content .mui-fullscreen {
top: 44px;
height: auto;
}
.mui-pull-top-tips {
position: absolute;
top: -20px;
left: 50%;
margin-left: -25px;
width: 40px;
height: 40px;
border-radius: 100%;
z-index: 1000;
}
.mui-bar~.mui-pull-top-tips {
top: 24px;
}
.mui-pull-top-wrapper {
width: 42px;
height: 42px;
display: block;
text-align: center;
background-color: #efeff4;
border: 1px solid #ddd;
border-radius: 25px;
background-clip: padding-box;
box-shadow: 0 4px 10px #bbb;
overflow: hidden;
}
.mui-pull-top-tips.mui-transitioning {
-webkit-transition-duration: 200ms;
transition-duration: 200ms;
}
.mui-pull-top-tips .mui-pull-loading {
/*-webkit-backface-visibility: hidden;
-webkit-transition-duration: 400ms;
transition-duration: 400ms;*/
margin: 0;
}
.mui-pull-top-wrapper .mui-icon,
.mui-pull-top-wrapper .mui-spinner {
margin-top: 7px;
}
.mui-pull-top-wrapper .mui-icon.mui-reverse {
/*-webkit-transform: rotate(180deg) translateZ(0);*/
}
.mui-pull-bottom-tips {
text-align: center;
background-color: #efeff4;
font-size: 15px;
line-height: 40px;
color: #777;
margin-bottom: 100px;/** 显示加载提示信息: 没有更多数据了等等。 */
}
.mui-pull-top-canvas {
overflow: hidden;
background-color: #fafafa;
border-radius: 40px;
box-shadow: 0 4px 10px #bbb;
width: 40px;
height: 40px;
margin: 0 auto;
}
.mui-pull-top-canvas canvas {
width: 40px;
}
.mui-slider-indicator.mui-segmented-control {
background-color: #efeff4;
}
</style>
</head>
<body style="background-color: #FFFFFF;">
<div class="mui-content" style="background-color:#fff;padding-bottom: 100px;" >
<div class="mui-content-padded" id="mui-content">
</div>
</div>
<!-- <h4>日巡检</h4>
<p>设置每日巡检一次,从当天起三个月内进行日巡检提醒。</p>
<h4>周巡检</h4>
<p>设置每周巡检一次,根据指定巡检日期进行提醒。</p>
<h4>月巡检</h4>
<p>设置每月巡检一次,根据指定巡检日期进行提醒。</p>
<h4>季度巡检</h4>
<p>设置每季度巡检一次,根据指定巡检日期进行提醒。</p>
<button type="button" class="mui-btn mui-btn-blue mui-btn-block add" >添加提醒按钮</button>
<button type="button" class="mui-btn mui-btn-blue mui-btn-block delete">删除提醒按钮</button> -->
<!-- <button type="button" class="mui-btn mui-btn-block" id="inspectionSettingsBtn" style="background-color: orangered;color: #FFFFFF;">巡检设置页面</button> -->
<script src="js/CryptoJS/aes.js"></script>
<script src="js/CryptoJS/md5.js"></script>
<script src="js/mui.min.js"></script>
<script src="js/app.js"></script>
<script src="js/mui.picker.min.js" type="text/javascript" charset="utf-8"></script>
<!-- <script src="js/mui.poppicker.js" type="text/javascript" charset="utf-8"></script> -->
<link href="css/mui.min.css" rel="stylesheet"/>
<link rel="stylesheet" type="text/css" href="css/mui.picker.min.css"/>
<!-- <link rel="stylesheet" type="text/css" href="css/mui.poppicker.css"/> -->
<script>
(function($, doc) {
$.init({
statusBarBackground: '#f7f7f7',
swipeBack:false //启用右滑关闭功能
});
var settings = {};
var loginInfo = {};
var inspectionSettingsData = [];
var setcalendar = function() {
$.toast('功能加载中,请稍后', {
type: 'div',
duration: 1000
});
};
var deletecalendar = function(){
$.toast('功能加载中,请稍后', {
type: 'div',
duration: 1000
});
};
$.plusReady(function() {
settings = app.getSettings();
if (settings && settings.loginInfo){
// loginInfo = JSON.parse(settings.loginInfo);
loginInfo = settings.loginInfo;
}else{
plus.nativeUI.alert("用户信息未知,需重新登录", function(){
plus.runtime.restart();
}, "系统提示", "确定");
return;
}
/**
* 获取巡检巡修设置信息
*/
function getInspectionSettings(){
var tokenData = localToken;
var encryptedLoginId = app.GetEncryptData(loginInfo.loginId + "", tokenData, tokenData);
var dataUrl = "loginId" + encryptedLoginId + "csrftoken" + tokenData;
var signData = app.GetSignData(dataUrl);
var postData = {
loginId: encryptedLoginId,
sign: signData
}
app.ajaxJson(
'/inspectionRecord/getInspectionSettings',
postData,
function(resultObject){
//请求成功处理
if (resultObject.code == 1000){
var dataList = resultObject.data;
// console.log("dataList: " + JSON.stringify(dataList));
inspectionSettingsData = dataList;
var muiContent = document.getElementById("mui-content");
var htmlContent = "";
for(var i = 0 ; i < dataList.length;i++){
var item = dataList[i];
var typeCode = item.typeCode;
if(typeCode == 1){
htmlContent += '<b4>' + item.title + '<b4>';
htmlContent += '<p>' + item.content + '</p>';
}else{
htmlContent += '<b4>' + item.title + '<b4>';
htmlContent += '<p>' + item.content + '</p>';
htmlContent += '<p>巡检日期:' + item.dates + '</p>';
}
}
htmlContent += '<button type="button" class="mui-btn mui-btn-blue mui-btn-block add" >添加巡检提醒</button>';
htmlContent += '<button type="button" class="mui-btn mui-btn-blue mui-btn-block delete">删除巡检提醒</button>';
htmlContent += '<p style="color: red;">系统提示:<br> 每两个月设置一次巡检提醒即可!设置添加提醒请先删除提醒设置。检查是否设置提醒:打开手机日历功能,点击某天查看日程是否有巡检提醒。 </p>';
muiContent.insertAdjacentHTML("beforeEnd" , htmlContent);
mui('.mui-content').on('tap', 'button.add', function() {
// var dateArray = [
// '2021-12-27 10:05',
// '2021-12-28 10:05',
// '2021-12-29 10:05'
// ];
// setcalendar('巡检提醒', '每天巡检', dateArray);
if (!inspectionSettingsData || inspectionSettingsData.length == 0){
plus.nativeUI.alert("", function(){}, "系统提示", "确定");
return;
}
plus.nativeUI.showWaiting('设置中,请等待...');
for (var j = 0 ; j < inspectionSettingsData.length;j++){
var item = inspectionSettingsData[j];
var title = item.title;
var datesArray = item.dates.split(",");
setcalendar(title, title, datesArray);
}
setTimeout(function(){
plus.nativeUI.closeWaiting();
plus.nativeUI.alert("设置提醒成功!请打开日历查看确认", function(){}, "系统提示", "确定");
},500);
});
mui('.mui-content').on('tap', 'button.delete', function() {
/** deletecalendar('巡检提醒'); */
// alert("删除巡检提醒");
plus.nativeUI.showWaiting('删除中,请等待...');
deletecalendar('每日巡检,每周巡检,每月巡检,每季度巡检');
// deletecalendar('每日巡检');
// deletecalendar('每周巡检');
// deletecalendar('每月巡检');
// deletecalendar('每季度巡检');
setTimeout(function(){
plus.nativeUI.closeWaiting();
plus.nativeUI.alert("删除提醒成功!请打开日历查看确认", function(){}, "系统提示", "确定");
},500);
});
}else if(resultObject.code == 1003){
//登录超时,跳转到登录页面
plus.nativeUI.alert(resultObject.msg, function(){
app.clearAllCache();
setTimeout(function(){
app.restartApp();
},1000);
}, "系统提示", "确定");
}
else{
plus.nativeUI.alert(resultObject.msg, function(){}, "系统提示", "确定");
return;
}
},
function(errorMsg){
//请求失败处理
plus.nativeUI.alert(errorMsg, function(){}, "系统提示", "确定");
},
""
);
}
getInspectionSettings();
var calanderURL = 'content://com.android.calendar/calendars',
eventsURL = 'content://com.android.calendar/events',
ContentValues = plus.android.importClass("android.content.ContentValues"),
Uri = plus.android.importClass('android.net.Uri'),
ContentUris = plus.android.importClass('android.content.ContentUris'),
Calendar = plus.android.importClass('java.util.Calendar'),
main = plus.android.runtimeMainActivity(),
userCursor = plus.android.invoke(main.getContentResolver(), 'query', Uri.parse(calanderURL), null, null, null, null),
userCursor_count = plus.android.invoke(userCursor, 'getCount'),
TimeZone = plus.android.importClass('java.util.TimeZone'),
TimeZone_str = plus.android.invoke(TimeZone.getDefault(), 'getID');
function setcalendar(title, description, dateArray) {
if(userCursor_count <= 0) {//如果没有日历账户
var account = new ContentValues(),
buildUpon = plus.android.invoke(Uri.parse(calanderURL), 'buildUpon'),
CalendarContract = plus.android.importClass('android.provider.CalendarContract');
plus.android.invoke(buildUpon, 'appendQueryParameter', CalendarContract.CALLER_IS_SYNCADAPTER, 'true');
plus.android.invoke(buildUpon, 'appendQueryParameter', 'account_name', 'someone@something.com');
// plus.android.invoke(buildUpon, 'appendQueryParameter', 'account_type', 'com.android.exchange');
plus.android.invoke(buildUpon, 'appendQueryParameter', 'account_type', 'LOCAL');
//设置账户信息
account.put('name', 'someone');
account.put('account_name', 'someone@something.com');
// account.put('account_type', 'com.android.exchange');
account.put('account_type', 'LOCAL');
account.put('calendar_displayName', 'someone_calendar');
account.put('visible', 1);
account.put('calendar_color', '-9206951');
account.put('calendar_access_level', '700');
account.put('sync_events', 1);
account.put('calendar_timezone', TimeZone_str);
account.put('ownerAccount', 'someone@something.com');
account.put('canOrganizerRespond', 0);
//保存账户信息
plus.android.invoke(main.getContentResolver(), 'insert', plus.android.invoke(buildUpon, 'build'), account);
//重新定义userCursor
userCursor = plus.android.invoke(main.getContentResolver(), 'query', Uri.parse(calanderURL), null, null, null, null);
//重新定义userCursor_count
userCursor_count++;
}
plus.android.invoke(userCursor, 'moveToLast');
var calId = plus.android.invoke(userCursor, 'getString', plus.android.invoke(userCursor, 'getColumnIndex', '_id')),
events = new ContentValues(),
mCalendar = Calendar.getInstance(),
date = null;
//date_str.split(/\s{1}|:|-/g)
for(var i = 0 ;i < dateArray.length;i++){
date = dateArray[i].split(/\s{1}|:|-/g);
plus.android.invoke(mCalendar, 'set', Calendar.YEAR, ~~date[0]);
plus.android.invoke(mCalendar, 'set', Calendar.MONTH, ((~~date[1]) - 1));
plus.android.invoke(mCalendar, 'set', Calendar.DATE, ~~date[2]);
// plus.android.invoke(mCalendar, 'set', Calendar.HOUR_OF_DAY, ~~date[3]);
// plus.android.invoke(mCalendar, 'set', Calendar.MINUTE, ~~date[4]);
plus.android.invoke(mCalendar, 'set', Calendar.HOUR_OF_DAY, 15);
plus.android.invoke(mCalendar, 'set', Calendar.MINUTE, 0);
var start = plus.android.invoke(plus.android.invoke(mCalendar, 'getTime'), 'getTime'),
end = plus.android.invoke(plus.android.invoke(mCalendar, 'getTime'), 'getTime');
//设置日历事件
events.put('title', title);
events.put('description', description);
events.put('calendar_id', calId);
events.put('dtstart', start);
events.put('dtend', end);
events.put('hasAlarm', 1);
events.put('eventTimezone', TimeZone_str);
var newEvent = plus.android.invoke(main.getContentResolver(), 'insert', Uri.parse('content://com.android.calendar/events'), events);
var id = plus.android.invoke(newEvent, 'getLastPathSegment');
var values = new ContentValues();
values.put('event_id', id);
values.put('minutes', '5');
values.put('method', '1');
plus.android.invoke(main.getContentResolver(), 'insert', Uri.parse('content://com.android.calendar/reminders'), values);
}
// $.toast('设置提醒成功');
// plus.nativeUI.alert("设置提醒成功!请打开日历查看确认", function(){}, "系统提示", "确定");
}
function deletecalendar(title) {
if(userCursor_count <= 0) {//如果没有日历账户
var account = new ContentValues(),
buildUpon = plus.android.invoke(Uri.parse(calanderURL), 'buildUpon'),
CalendarContract = plus.android.importClass('android.provider.CalendarContract');
plus.android.invoke(buildUpon, 'appendQueryParameter', CalendarContract.CALLER_IS_SYNCADAPTER, 'true');
plus.android.invoke(buildUpon, 'appendQueryParameter', 'account_name', 'someone@something.com');
plus.android.invoke(buildUpon, 'appendQueryParameter', 'account_type', 'LOCAL');
//设置账户信息
account.put('name', 'someone');
account.put('account_name', 'someone@something.com');
account.put('account_type', 'LOCAL');
account.put('calendar_displayName', 'someone_calendar');
account.put('visible', 1);
account.put('calendar_color', '-9206951');
account.put('calendar_access_level', '700');
account.put('sync_events', 1);
account.put('calendar_timezone', TimeZone_str);
account.put('ownerAccount', 'someone@something.com');
account.put('canOrganizerRespond', 0);
//保存账户信息
plus.android.invoke(main.getContentResolver(), 'insert', plus.android.invoke(buildUpon, 'build'), account);
}
//定义EventCursor用于删除
var userCursor2 = plus.android.invoke(main.getContentResolver(), 'query', Uri.parse(eventsURL), null, null, null, null);
// console.log("删除巡检日历信息1.....");
deleteitem = title;
plus.android.invoke(userCursor2, 'moveToFirst');
while(!plus.android.invoke(userCursor2, 'isAfterLast')){
var eventTitle = plus.android.invoke(userCursor2,"getString", plus.android.invoke(userCursor2, 'getColumnIndex', 'title'));
//if(title == eventTitle ){
if(title.indexOf(eventTitle) != -1){
// console.log("删除巡检日历信息2: title = {}....." , eventTitle);
var id = plus.android.invoke(userCursor2, 'getInt',plus.android.invoke(userCursor2,'getColumnIndex','_id'));
var deleteUri = ContentUris.withAppendedId(Uri.parse(eventsURL), id);
var rows = plus.android.invoke(main.getContentResolver(),"delete",deleteUri,null,null);
if (rows == -1) {
//事件删除失败
$.toast("删除失败");
return;
}
// $.toast("删除成功");
}
plus.android.invoke(userCursor2, 'moveToNext');
}
}
// mui('.mui-content').on('tap', 'button.add', function() {
// var dtPicker = new $.DtPicker();
// // dtPicker.show(function(rs) {
// // //setcalendar('日巡检提醒', '每天巡检', rs.text);
// // //deletecalendar('测试提醒标题');
// // alert("选择的日期: " + rs.text);
// // dtPicker.dispose();
// // });
// var dateArray = [
// '2021-12-27 10:05',
// '2021-12-28 10:05',
// '2021-12-29 10:05'
// ];
// setcalendar('巡检提醒', '每天巡检', dateArray);
// });
});
}(mui, document));
</script>
</body>
</html>
代码说明:
1、我这边是从后台读取的提醒设置数据(就是按日、周、月、季度):就是平时的日期格式 比如:2024-01-17
/**
* 获取巡检巡修设置信息
*/
function getInspectionSettings(){
...省略其他代码
//请求成功处理
if (resultObject.code == 1000){
var dataList = resultObject.data;
// console.log("dataList: " + JSON.stringify(dataList));
inspectionSettingsData = dataList;
}
...省略其他代码
}
dataList数据格式:
dataList: [{"dates":"2024-01-17,2024-01-18,2024-01-19,2024-01-20,2024-01-21,2024-01-22,2024-01-23,2024-01-24,2024-01-25,2024-01-26,2024-01-27,2024-01-28,2024-01-29,2024-01-30,2024-01-31,2024-02-01,2024-02-02,2024-02-03,2024-02-04,2024-02-05,2024-02-06,2024-02-07,2024-02-08,2024-02-09,2024-02-10,2024-02-11,2024-02-12,2024-02-13,2024-02-14,2024-02-15,2024-02-16,2024-02-17,2024-02-18,2024-02-19,2024-02-20,2024-02-21,2024-02-22,2024-02-23,2024-02-24,2024-02-25,2024-02-26,2024-02-27,2024-02-28,2024-02-29,2024-03-01,2024-03-02,2024-03-03,2024-03-04,2024-03-05,2024-03-06,2024-03-07,2024-03-08,2024-03-09,2024-03-10,2024-03-11,2024-03-12,2024-03-13,2024-03-14,2024-03-15,2024-03-16","title":"每日巡检","content":"设置每日巡检一次,从当天起2个月内进行日巡检提醒。","typeCode":1},{"dates":"2024-01-23,2024-01-30,2024-02-06,2024-02-13,2024-02-20,2024-02-27,2024-03-05,2024-03-12","title":"每周巡检","content":"设置每周巡检一次,根据指定巡检日期进行提醒。","typeCode":2},{"dates":"2024-02-01,2024-02-02,2024-03-01,2024-03-02","title":"每月巡检","content":"设置每月巡检一次,根据指定巡检日期进行提醒。","typeCode":3},{"dates":"2024-02-15,2024-03-15","title":"每季度巡检","content":"设置每季度巡检一次,根据指定巡检日期进行提醒。","typeCode":4}]
2、因为提醒类型不同且每类有多条记录,对应不同的日历事件(提醒)。所以需要循环进行设置:
plus.nativeUI.showWaiting('设置中,请等待...');
for (var j = 0 ; j < inspectionSettingsData.length;j++){
var item = inspectionSettingsData[j];
var title = item.title;
var datesArray = item.dates.split(",");
setcalendar(title, title, datesArray);
}
function setcalendar(title, description, dateArray) {
if(userCursor_count <= 0) {//如果没有日历账户
var account = new ContentValues(),
buildUpon = plus.android.invoke(Uri.parse(calanderURL), 'buildUpon'),
CalendarContract = plus.android.importClass('android.provider.CalendarContract');
plus.android.invoke(buildUpon, 'appendQueryParameter', CalendarContract.CALLER_IS_SYNCADAPTER, 'true');
plus.android.invoke(buildUpon, 'appendQueryParameter', 'account_name', 'someone@something.com');
// plus.android.invoke(buildUpon, 'appendQueryParameter', 'account_type', 'com.android.exchange');
plus.android.invoke(buildUpon, 'appendQueryParameter', 'account_type', 'LOCAL');
//设置账户信息
account.put('name', 'someone');
account.put('account_name', 'someone@something.com');
// account.put('account_type', 'com.android.exchange');
account.put('account_type', 'LOCAL');
account.put('calendar_displayName', 'someone_calendar');
account.put('visible', 1);
account.put('calendar_color', '-9206951');
account.put('calendar_access_level', '700');
account.put('sync_events', 1);
account.put('calendar_timezone', TimeZone_str);
account.put('ownerAccount', 'someone@something.com');
account.put('canOrganizerRespond', 0);
//保存账户信息
plus.android.invoke(main.getContentResolver(), 'insert', plus.android.invoke(buildUpon, 'build'), account);
//重新定义userCursor
userCursor = plus.android.invoke(main.getContentResolver(), 'query', Uri.parse(calanderURL), null, null, null, null);
//重新定义userCursor_count
userCursor_count++;
}
plus.android.invoke(userCursor, 'moveToLast');
var calId = plus.android.invoke(userCursor, 'getString', plus.android.invoke(userCursor, 'getColumnIndex', '_id')),
events = new ContentValues(),
mCalendar = Calendar.getInstance(),
date = null;
//date_str.split(/\s{1}|:|-/g)
for(var i = 0 ;i < dateArray.length;i++){
date = dateArray[i].split(/\s{1}|:|-/g);
plus.android.invoke(mCalendar, 'set', Calendar.YEAR, ~~date[0]);
plus.android.invoke(mCalendar, 'set', Calendar.MONTH, ((~~date[1]) - 1));
plus.android.invoke(mCalendar, 'set', Calendar.DATE, ~~date[2]);
// plus.android.invoke(mCalendar, 'set', Calendar.HOUR_OF_DAY, ~~date[3]);
// plus.android.invoke(mCalendar, 'set', Calendar.MINUTE, ~~date[4]);
plus.android.invoke(mCalendar, 'set', Calendar.HOUR_OF_DAY, 15);
plus.android.invoke(mCalendar, 'set', Calendar.MINUTE, 0);
var start = plus.android.invoke(plus.android.invoke(mCalendar, 'getTime'), 'getTime'),
end = plus.android.invoke(plus.android.invoke(mCalendar, 'getTime'), 'getTime');
//设置日历事件
events.put('title', title);
events.put('description', description);
events.put('calendar_id', calId);
events.put('dtstart', start);
events.put('dtend', end);
events.put('hasAlarm', 1);
events.put('eventTimezone', TimeZone_str);
var newEvent = plus.android.invoke(main.getContentResolver(), 'insert', Uri.parse('content://com.android.calendar/events'), events);
var id = plus.android.invoke(newEvent, 'getLastPathSegment');
var values = new ContentValues();
values.put('event_id', id);
values.put('minutes', '5');
values.put('method', '1');
plus.android.invoke(main.getContentResolver(), 'insert', Uri.parse('content://com.android.calendar/reminders'), values);
}
// $.toast('设置提醒成功');
// plus.nativeUI.alert("设置提醒成功!请打开日历查看确认", function(){}, "系统提示", "确定");
}
3、注意日历提醒时间是可以改的,我这边设置的是下午 3 点(15:00:00)【所有提醒事件都在该时间左右进行提醒】
plus.android.invoke(mCalendar, 'set', Calendar.HOUR_OF_DAY, 15);
plus.android.invoke(mCalendar, 'set', Calendar.MINUTE, 0);
4、效果图
添加日历提醒
日历提醒(锁屏状态下)
删除日历提醒:
5、相关问题
5.1 手机APP需要打包后才能使用日历功能
5.2 手机APP需要开启日历权限访问,否则在添加、删除日历提醒时可能卡住或无响应。