菜鸡
菜鸡
  • 发布:2017-09-03 01:06
  • 更新:2022-04-12 13:12
  • 阅读:8616

mui(准确的说是html5+)Android原生日历提醒插入案例踩坑经历

分类:HTML5+
  • 缘起
    需要在app里弄个定时提醒功能
  • 过程
    在问答社区看到一个大佬分享的安卓插入日历提醒的按理
    点击这里过去看看???
  • 但是
    看得有点晕,很多代码都不知道在干嘛(毕竟不懂原生)
    开始的时候就原样复制了一遍代码,也没怎么看,心想着能把项目完成了再说
  • 后来发现
    没登录日历账号的手机不能设置提醒,反正就是各种报错
    好了,不说心路历程了
    直接上干货
    (html和引入date picker就省略了哈)
    (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);
  • 最后说一句
  • 打包的时候记得勾选日历权限
  • 这个问题坑死我了
5 关注 分享
zapoi 赵梦欢 水灵退散 lhyh 咕噜啦

要回复文章请先登录注册

崔尐磊

崔尐磊

回复 崔尐磊 :
出来了,但是ios的怎么搞啊
2018-03-12 18:22
崔尐磊

崔尐磊

为什么我这直接复制的回报new $.DtPicker() 不是一个构造函数呢,求了解的大神指导一波
2018-03-12 16:19
菜鸡

菜鸡 (作者)

回复 FOFxjc :
老哥,厉害了,你是不是会原生开发
2017-10-30 13:50
FOFxjc

FOFxjc

非常有用,借鉴之后,补充了删除提醒的功能,成功满足了需求,代码如下,望指正
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title></title>
<script src="js/mui.min.js"></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 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>
<script type="text/javascript" charset="utf-8">
mui.init();
(function($) {
var setcalendar = function() {
$.toast('功能加载中,请稍后', {
type: 'div',
duration: 1000
});
};
var deletecalendar = function(){
$.toast('功能加载中,请稍后', {
type: 'div',
duration: 1000
});
};
$.plusReady(function() {
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');
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('设置提醒成功');
}
deletecalendar = function(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', '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);
}
//定义EventCursor用于删除
var userCursor2 = plus.android.invoke(main.getContentResolver(), 'query', Uri.parse(eventsURL), null, null, null, null);

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 ){
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');
}
}
});
$.plusReady(function() {
mui('.mui-content').on('tap', 'button.add', function() {
alert("1111");
var dtPicker = new $.DtPicker();
dtPicker.show(function(rs) {
setcalendar('测试提醒标题', '测试提醒内容', rs.text);
//deletecalendar('测试提醒标题');
dtPicker.dispose();
});
});
mui('.mui-content').on('tap', 'button.delete', function() {
deletecalendar('测试提醒标题');
var dtPicker = new $.DtPicker();
});
});
})(mui);
</script>
</head>
<body>
<div class="mui-content">
<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>
</div>
</body>
</html>
2017-10-28 11:42
5***@qq.com

5***@qq.com

我鸡屌的一匹
2017-09-04 16:11
lhyh

lhyh

鸡哥V5
2017-09-04 09:43
水灵退散

水灵退散

6666
2017-09-04 09:32
zapoi

zapoi

鸡哥,iOS的呢
2017-09-04 09:03