国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Cordova插件開發之自定義消息事件addEventListener,fireWindowEvent

2019-11-09 15:17:08
字體:
來源:轉載
供稿:網友

關于在cordova的插件開發的做定義消息的功能沒有官方的介紹文章,但是官方有一個這樣的例子,就是電源狀態插件。我們通過學習這個插件的編寫來學習自定義消息。

這個插件的地址為:https://github.com/apache/cordova-plugin-battery-status

 

安裝方法為:cordova plugin add cordova-plugin-battery-status

 

 

使用方法是:在自己的js工程里執行以下幾句話:

window.addEventListener("batterystatus", onBatteryStatus, false); function onBatteryStatus(status) {    console.log("Level: " + status.level + " isPlugged: " + status.isPlugged);}

分別是給工程添加插件自定義的消息事件,并指定消息響應函數

 

在我的測試例子中,使用了如下函數為兩個按鍵的響應函數,一個訂閱消息,一個取消訂閱:

function onBatteryStatus(info) {  alert("BATTERY STATUS:  Level: " + info.level + "isPlugged: " + info.isPlugged);}$scope.onbatteryStartBtn = function() {  alert("onStartBtn");  window.addEventListener("batterystatus",onBatteryStatus, false);};$scope.onbatteryStopBtn = function() {  alert("onStopBtn");  window.removeEventListener("batterystatus",onBatteryStatus);};

 

 

然后編譯工程:ionic build android

在手機上測試:ionic run android

 

這里顯然就是典型的回調機制,就是在自己的工程里定制消息的響應回調函數,然后當產生這個消息的話,回調函數就會被調用。

那么接下來看一下這個電池狀態插件是怎樣實現的。

 

var cordova = require('cordova'),    exec = require('cordova/exec');var STATUS_CRITICAL = 5;var STATUS_LOW = 20;var Battery = function() {    this._level = null;    this._isPlugged = null;    // Create new event handlers on thewindow (returns a channel instance)    this.channels = {      batterystatus:cordova.addWindowEventHandler("batterystatus"),      batterylow:cordova.addWindowEventHandler("batterylow"),      batterycritical:cordova.addWindowEventHandler("batterycritical")    };    for (var key in this.channels) {        this.channels[key].onHasSubscribersChange= Battery.onHasSubscribersChange;    }};function handlers() {    return battery.channels.batterystatus.numHandlers+        battery.channels.batterylow.numHandlers+        battery.channels.batterycritical.numHandlers;}/** * Event handlers for when callbacks getregistered for the battery. * Keep track of how many handlers wehave so we can start and stop the native battery listener * apPRopriately (and hopefully save onbattery life!). */Battery.onHasSubscribersChange = function() {  // If we just registered the firsthandler, make sure native listener is started.  if (this.numHandlers === 1 && handlers()=== 1) {      exec(battery._status, battery._error,"Battery", "start", []);  } else if (handlers() === 0) {      exec(null, null, "Battery","stop", []);  }};/** * Callback for battery status * * @param {Object} info            keys: level, isPlugged */Battery.prototype._status = function (info) {    if (info) {        if (battery._level !== info.level|| battery._isPlugged !== info.isPlugged) {                        if(info.level === null &&battery._level !== null) {                return; // special casewhere callback is called because we stopped listening to the native side.            }                        // Something changed. Firebatterystatus event            cordova.fireWindowEvent("batterystatus",info);            if (!info.isPlugged) { // donot fire low/critical if we are charging. issue: CB-4520                // note the following areNOT exact checks, as we want to catch a transition from                // above the threshold tobelow. issue: CB-4519                if (battery._level >STATUS_CRITICAL && info.level <= STATUS_CRITICAL) {                    // Fire criticalbattery event                    cordova.fireWindowEvent("batterycritical",info);                }                else if (battery._level >STATUS_LOW && info.level <= STATUS_LOW) {                    // Fire low battery event                    cordova.fireWindowEvent("batterylow",info);                }            }            battery._level = info.level;            battery._isPlugged = info.isPlugged;        }    }};/** * Error callback for battery start */Battery.prototype._error = function(e) {    console.log("Error initializingBattery: " + e);};var battery = new Battery(); // jshint ignore:linemodule.exports = battery;

 

其中,exec(battery._status, battery._error, "Battery","start", []);

用來讓插件的js代碼和硬件本地代碼聯系起來。

整個插件的工作原理是:

 

1. 定義自定義消息

2. 調用端增加對自定義消息的接聽(addEventListener)

3. 本地產生消息,通過exec函數指定的回調函數來響應。,然后在相應函數里,激發自定義消息。(fireWindowEvent)

4. 接聽者收到消息和參數

 

其中自定義消息部分是:

// Create newevent handlers on the window (returns a channel instance)    this.channels = {      batterystatus:cordova.addWindowEventHandler("batterystatus"),      batterylow:cordova.addWindowEventHandler("batterylow"),      batterycritical:cordova.addWindowEventHandler("batterycritical")    };    for (var key in this.channels) {        this.channels[key].onHasSubscribersChange= Battery.onHasSubscribersChange;    }};

 

這里共定義了三條消息。格式參考以上格式,

 

 

當有人注冊這個自定義消息的時候會響應以下函數:

/** * Event handlers for when callbacks getregistered for the battery. * Keep track of how many handlers wehave so we can start and stop the native battery listener * appropriately (and hopefully save onbattery life!). */Battery.onHasSubscribersChange = function() {  // If we just registered the firsthandler, make sure native listener is started.  if (this.numHandlers === 1 && handlers()=== 1) {      exec(battery._status, battery._error,"Battery", "start", []);  } else if (handlers() === 0) {      exec(null, null, "Battery","stop", []);  }};

在函數里執行exec函數來完成插件與本地原生代碼的連接。使用exec函數調用原生代碼,然后原始代碼返回結果,在這個例子中,原始代碼保持了返回數據的通道,使得,原生代碼一檢測到電量變化,就立馬使用這個保持的通道,來告知插件電量的變化,,然后插件激發自定義消息,。這樣用戶界面就會收到電量變化的事件。

 

本文僅提供思路與方法,具體定制自定義消息,需要深入閱讀并測試這個插件的源代碼。

插件源碼地址:https://github.com/apache/cordova-plugin-battery-status


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 深州市| 锦屏县| 甘孜县| 民乐县| 罗山县| 伊川县| 陆丰市| 永吉县| 旬邑县| 福鼎市| 揭西县| 耿马| 海南省| 台北市| 高台县| 开远市| 喀喇沁旗| 贺州市| 边坝县| 遵义市| 庐江县| 霍邱县| 察隅县| 大方县| 宁河县| 临猗县| 徐水县| 波密县| 岫岩| 台前县| 运城市| 修武县| 肇源县| 阜南县| 大渡口区| 黔东| 班戈县| 无为县| 惠东县| 常州市| 淳化县|