H5+ 操做手機日曆

參考文章 https://ask.dcloud.net.cn/article/215  使用native.js操做安卓原生系統java

 http://www.javashuo.com/article/p-rxrviyhg-n.html 在android中 操做日曆android

使用的app

content://com.android.calendar/calendars  系統日曆帳號表ide

content://com.android.calendar/events 系統日曆事件表ui

content://com.android.calendar/reminders 系統提現表spa

 

 獲取系統日曆權限

 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.net

而後經過calendar_id插入日曆事件,返回事件ID,根據事件ID 插入日曆提醒,代碼以下:code

/*
 * 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 }

完整代碼blog

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

相關文章
相關標籤/搜索