与离子framwork科尔多瓦亮度插件
问题描述:
我使用https://github.com/mgcrea/cordova-plugin-brightness与离子framwork科尔多瓦亮度插件
我已经想通了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();但是之后?我如何处理成功或失败?
答
getBrightness()
应该处理成功和错误回调函数。成功回调将返回亮度设置的值。该值将是一个范围从0到1的浮点数。在系统默认亮度的情况下,该值返回-1。
结帐我在香草科尔多瓦项目中尝试这个简单的示例代码:
的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;
}
}
}
而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;
}
}
});
Thanks Gandhi!在Ionic下面发布我的解决方案。 –
@彼得有同样的彼得。如果它有帮助,你也可以upvote。 – Gandhi