Cordova亮度插件采用离子骨架

时间:2016-06-13 21:46:29

标签: javascript cordova ionic-framework cordova-plugins

我正在使用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();但是之后?我如何处理成功或失败?

2 个答案:

答案 0 :(得分:5)

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");
    }
}

答案 1 :(得分:2)

谢谢!将它转换为我的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;
        }
    }
}

和get函数

可能会将此移至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;
      }
    }  
});