一個Notification 進度條插件(android,NJS實現,直接就可以使用)

參考文章:http://ask.dcloud.net.cn/article/503android

源碼地址下載api

如題,分享一個Notification 進度條插件(android,用js調用原生api實現,直接就能夠使用).
參考1: http://ask.dcloud.net.cn/article/155
參考2:http://ask.dcloud.net.cn/question/2464數組

詳細介紹: 最近有一個需求是,android更新資源包時,須要在通知欄裏顯示下載進度條,因而就搜索了下已有的解決方案
發現並無現成的解決方案,因而參考了以上兩個解決方案,結合了下,實現了直接能調用的js插件.app

第一步(分析以上兩種方案的缺陷):
第一種:功能上能實現需求,可是使用起來太麻煩,並且也不方便調試(須要離線打包後才能看到效果),
並且,我這裏還遇到了一個問題,在 hbuild 6.9.2 中,我明明已經添加了自定義模塊權限,可是卻一直提示我 ***權限沒有添加, 真的是把我弄的吐血了, 因此沒辦法,只能另謀他路.函數

第二種:使用起來很方便,可是卻沒有徹底達到功能要求(只顯示了普統統知,但沒有顯示進度條),並且在api <16 的機子上會報錯.
因而在這個基礎上,結合第一種方法.寫了一個 工具類,實現android的通知欄控制(兼容了api11-16的通知顯示)工具

源碼:測試

/**
 * @description njs實現android原生功能 
 * 1.通知欄消息
 * @see http://ask.dcloud.net.cn/article/503
 * 
 * @author dailc
 * @version 1.0
 * @time 2016-01-08 08:38:20
 */
(function(obj) {
    var defaultTitle = '通知欄標題';
    var defaultContent = '通知內容';
    var defaultTicker = '通知提示';
    var defaultNotifyId = 1000;
    var defaultNumber = 1;
    /**
     * plusReady
     * @param {type} callback
     * @returns {Window}
     */
    obj.plusReady = function(callback) {
        if (window.plus) {
            setTimeout(function() { //解決callback與plusready事件的執行時機問題(典型案例:showWaiting,closeWaiting)
                callback();
            }, 0);
        } else {
            document.addEventListener("plusready", function() {
                callback();
            }, false);
        }
        return this;
    };
    /**
     * @description 比較兩個版本大小
     * 比較版本大小,若是新版本nowVersion大於舊版本OldResourceVersion則返回true,不然返回false
     */
    function compareVersion(OldVersion, nowVersion) {
        if (!OldVersion || !nowVersion || OldVersion == '' || nowVersion == '') {

            return false;
        }
        //第二份參數 是 數組的最大長度
        var OldVersionA = OldVersion.split(".", 4);
        var nowVersionA = nowVersion.split(".", 4);
        for (var i = 0; i < OldVersionA.length && i < nowVersionA.length; i++) {
            var strOld = OldVersionA[i];
            var numOld = parseInt(strOld);
            var strNow = nowVersionA[i];
            var numNow = parseInt(strNow);
            //小版本到高版本
            if (numNow > numOld
                //||strNow.length>strOld.length
            ) {
                return true;
            } else if (numNow < numOld) {
                return false;
            }
        }
        //若是是版本  如 1.6 - 1.6.1
        if (nowVersionA.length > OldVersionA.length && 0 == nowVersion.indexOf(OldVersion)) {
            return true;
        }
    };
    /**
     * @description 經過push功能來推送消息
     */
    obj.sendNotificationByPush = function() {
        var options = {
            cover: false
        };
        var str = ": 歡迎使用Html5 Plus建立本地消息!";
        plus.push.createMessage(str, "LocalMSG", options);
    };
    (function() {
        /**
         * @constructor 建立通知欄進度條構造函數
         */
        function NotificationCustom() {
            if (plus.os.name != 'Android') {
                return;
            }
            //當前版本號
            var SystemVersion = plus.os.version;
            var Context = plus.android.importClass("android.content.Context");
            var main = plus.android.runtimeMainActivity();
            var NotificationManager = plus.android.importClass("android.app.NotificationManager");
            var nm = main.getSystemService(Context.NOTIFICATION_SERVICE)
                // Notification build 要android api16以上才能使用(4.1.2以上)
            var Notification = null;
            if (compareVersion('4.1.1', SystemVersion) == true) {
                Notification = plus.android.importClass("android.app.Notification");
            } else {
                Notification = plus.android.importClass("android.support.v4.app.NotificationCompat");
            }
            if (Notification) {
                this.notifyManager = nm;
                this.mNotificationBuild = new Notification.Builder(main);
                //設爲true表明常駐狀態欄
                this.mNotificationBuild.setOngoing(false);
                this.mNotificationBuild.setContentTitle(defaultTitle);
                this.mNotificationBuild.setContentText(defaultContent);
                this.mNotificationBuild.setTicker(defaultTicker);
                //默認的push圖標
                this.mNotificationBuild.setSmallIcon(17301620);
                //設置默認聲音
                //console.log('默認:'+plus.android.importClass("android.app.Notification").DEFAULT_SOUND);
                this.mNotificationBuild.setDefaults(plus.android.importClass("android.app.Notification").DEFAULT_SOUND);
                //this.mNotificationBuild.setNumber(defaultNumber)
            }
        };
        /**
         * @description 給android通知欄發送通知
         * @param {String} title 標題
         * @param {String} content  內容
         * @param {String} tickerTips  提示
         * @param {Number} notifyId id,默認爲1000
         */
        NotificationCustom.prototype.setNotification = function(title, content, tickerTips,notifyId) {
            if (this.mNotificationBuild == null ||
                this.notifyManager == null) {
                return;
            }
            notifyId = (typeof(notifyId)=='number')?notifyId:defaultNotifyId;
            title = title || defaultTitle;
            content = content || defaultContent;
            tickerTips = tickerTips || defaultTicker;
            this.mNotificationBuild.setContentTitle(title);
            this.mNotificationBuild.setContentText(content);
            this.mNotificationBuild.setTicker(tickerTips);
            //默認有聲音
            this.mNotificationBuild.setDefaults(plus.android.importClass("android.app.Notification").DEFAULT_SOUND);
            this.notifyManager.notify(notifyId, this.mNotificationBuild.build());
        };
        /**
         * @description 設置進度條
         * @param {Number} progress
         * @param {String} title 標題
         * @param {String} content  內容
         * @param {String} tickerTips  提示
         * @param {Number} notifyId id,默認爲1000
         */
        NotificationCustom.prototype.setProgress = function(progress, title, content, tickerTips,notifyId) {
            if (this.mNotificationBuild == null ||
                this.notifyManager == null) {
                return;
            }
            notifyId = (typeof(notifyId)=='number')?notifyId:defaultNotifyId;
            title = title || '正在下載';
            content = content || '正在下載';
            tickerTips = tickerTips || '進度提示';
            //          tickerTips = tickerTips || defaultTicker;
            this.mNotificationBuild.setContentTitle(title);
            this.mNotificationBuild.setContentText(content);
            this.mNotificationBuild.setTicker(tickerTips);
            //進度條顯示時,默認無聲音
            this.mNotificationBuild.setDefaults(0);
            this.mNotificationBuild.setProgress(100, progress, false);
            this.notifyManager.notify(notifyId, this.mNotificationBuild.build());
        };
        /**
         * @description 完成進度條
         * @param {String} title 標題
         * @param {String} content  內容
         * @param {String} tickerTips  提示
         * @param {Number} notifyId id,默認爲1000
         */
        NotificationCustom.prototype.compProgressNotification = function(title, content, tickerTips,notifyId) {
            if (this.mNotificationBuild == null ||
                this.notifyManager == null) {
                return;
            }
            notifyId = (typeof(notifyId)=='number')?notifyId:defaultNotifyId;
            title = title || '進度條顯示完畢';
            content = content || '進度條顯示完畢';
            tickerTips = tickerTips || '進度提示';
            this.mNotificationBuild.setContentTitle(title);
            this.mNotificationBuild.setContentText(content);
            this.mNotificationBuild.setTicker(tickerTips);
            this.mNotificationBuild.setProgress(0, 0, false);
            //默認有聲音
            this.mNotificationBuild.setDefaults(plus.android.importClass("android.app.Notification").DEFAULT_SOUND);
            this.notifyManager.notify(notifyId, this.mNotificationBuild.build());
        };
        /**
         * @description 清除通知欄信息
         * @param {Number} notifyId id,默認爲1000
         */
        NotificationCustom.prototype.clearNotification = function(notifyId) {
            notifyId = (typeof(notifyId)=='number')?notifyId:defaultNotifyId;
            if(this.notifyManager){
                this.notifyManager.cancel(notifyId);
            }       
        };
        /**
         * @description 清除全部通知欄信息
         */
        NotificationCustom.prototype.clearAllNotification = function() {
            if(this.notifyManager){
                this.notifyManager.cancelAll();
            }       
        };
        obj.plusReady(function() {
            obj.NotificationUtil = new NotificationCustom();
        });
    })();

})(window.NjsPhoneApi = {});

調用方法示例:
顯示普統統知:ui

NjsPhoneApi.NotificationUtil.setNotification('測試標題'+staticI,'測試內容');

顯示進度條代碼:this

function testProgress() {
        //插件調用
        NjsPhoneApi.NotificationUtil.setNotification("新版下載", "開始下載");
        var current = 0;
        NjsPhoneApi.NotificationUtil.setProgress(current); //插件調用
        function progress() {
            setTimeout(function() {
                current += 1;
                NjsPhoneApi.NotificationUtil.setProgress(current);
                if(current>=100){
                     NjsPhoneApi.NotificationUtil.compProgressNotification("下載完成");
                }else{
                    progress();
                }
            }, 100);
        };
        progress();
    };
testProgress();//調用顯示進度條

取消單條通知:(傳入參數爲id,不傳採用默認id)spa

NjsPhoneApi.NotificationUtil.clearNotification();

取消全部通知:

NjsPhoneApi.NotificationUtil.clearAllNotification();

另外: 支持自定義id的通知,也就是說能夠經過傳入不一樣的id,同時顯示不一樣的通知

效果圖1:
效果圖2:
示例源碼:鑑於有一些朋友會有各式各樣的奇怪錯誤,因此這裏單獨寫了一個示例,測試了android機型(華爲,聯想)都是能夠正常使用的.
示例源碼中採用的是默認id

相關文章
相關標籤/搜索