簡體   English   中英

Cordova亮度插件采用離子骨架

[英]Cordova brightness plugin with ionic framework

我正在使用https://github.com/mgcrea/cordova-plugin-brightness

我已經想出了setBrightness可以在Ionic app中使用。 但我無法弄清楚getBrightness是否正常工作。 關於如何讓它工作的任何指針都表示贊賞。

這是我的setBrightness:

  $scope.changeBrightness = function (newBrightness) {
    myBrightness = parseFloat(newBrightness)/1000;
    if (window.cordova && window.cordova.plugins.brightness) {
      var LightControl = cordova.plugins.brightness;
      LightControl.setBrightness(myBrightness);
    }
  }

LightControl.getBrightness(); 但是之后? 我如何處理成功或失敗?

getBrightness()應該處理成功和錯誤回調函數。 成功回調將返回亮度設置的值。 此值將是一個從0到1的浮點數。如果系統默認亮度,則返回-1。

查看我在vanilla cordova項目中嘗試的這個簡單的示例代碼:

的index.html

<!DOCTYPE html>
<html>
    <head>        
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>Brightness Control</title>
    </head>
    <body>      
        <br>        
        <br>
        Set Brightness <input type="button" value="setbright" name="Wifi" id="setbright"/>   <br>
        Get Brightness <input type="button" value="getbright" name="Wifi" id="getbright"/>   <br>
        <script type="text/javascript" src="js/jquery.js"></script> 
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/app.js"></script>
    </body>
</html>

app.js

$(document).ready(function() {
    document.addEventListener("deviceready", onDeviceReady, false);
});

function onDeviceReady() {      

     $('#setbright').click( function() 
        {   
            try {               
                cordova.plugins.brightness.setBrightness(0.9, setsuccess, seterror);
            }
            catch(err) {
                alert("Plugin Error - " + err.message);
            }

        }); 

     $('#getbright').click( function() 
        {   
            try {               
                cordova.plugins.brightness.getBrightness(getsuccess, geterror);
            }
            catch(err) {
                alert("Plugin Error - " + err.message);
            }

        }); 

    function setsuccess(e) {        
        alert("Brightness set successfully");
    }

    function getsuccess(e) {                
        alert("Brightness value - " + e);
    }

    function seterror(e) {
        alert("Error setting brightness");
    }

    function geterror(e) {
        alert("Error getting brightness");
    }
}

謝謝! 將它轉換為我的Ionic項目並使set函數處理回調。

page.html中

  <div class="item range range-light">
  <span class="smallA"><i class="ion-ios-sunny-outline"></i></span>
  <input id="range" type="range" min="0" max="1000" ng-model="brightness" ng-change="setBrightness(brightness)">
  <span class="bigA"><i class="ion-ios-sunny"></i></span>
</div>

controller.js

和設置功能

  $scope.setBrightness = function (newBrightness) {
    myBrightness = parseFloat(newBrightness)/1000;
    if (window.cordova && window.cordova.plugins.brightness) {
            var LightControl = cordova.plugins.brightness;
            try {
            LightControl.setBrightness(myBrightness, setsuccess, seterror);
        }
        catch(err) {
            console.log("setBrightness", err);
        }
        function seterror(e) {
        console.log("seterror", e);
        }

        function setsuccess(e) {
        console.log("setsuccess", e);
            var brightness = Math.round(e*1000);
            $scope.brightness = brightness;
        }
    }
}

和獲取功能

可能會將此移至app.js,因為我可能需要在其他控制器中使用它。

$scope.$on('$ionicView.enter', function(){
if (window.cordova && window.cordova.plugins.brightness) {
    var LightControl = cordova.plugins.brightness;
    try {
      LightControl.getBrightness(getsuccess, geterror);
    }
    catch(err) {
      console.log("getBrightness", err);
    }

  function geterror(e) {
      console.log("geterror", e);
  }

  function getsuccess(e) {
    //alert("Brightness value - " + e);
    var brightness = Math.round(e*1000);
    //alert("Brightness value - " + brightness);
      $scope.brightness = brightness;
      }
    }  
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM