【问题标题】:Cordova brightness plugin with ionic framework带有离子框架的 Cordova 亮度插件
【发布时间】:2016-10-14 10:57:23
【问题描述】:

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

我已经弄清楚了在 Ionic 应用程序中工作的 setBrightness。 但我无法弄清楚 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();但是之后?如何处理成功或失败?

【问题讨论】:

    标签: javascript cordova ionic-framework cordova-plugins


    【解决方案1】:

    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 中发布我的解决方案。
    • @Peter 对同一个 Peter 投了赞成票。如果有帮助,您也可以投票。
    【解决方案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;
          }
        }  
    });
    

    【讨论】:

      猜你喜欢
      • 2021-01-29
      • 1970-01-01
      • 1970-01-01
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-23
      • 1970-01-01
      相关资源
      最近更新 更多