检查使用PhoneGap时Android版本是否低于4?

4

我正在尝试使用PhoneGap检查Android版本是否低于4。

我使用以下代码获取版本号。

   var deviceOS  = device.platform;  //fetch the device operating system
   var deviceOSVersion = device.version;  //fetch the device OS version
          alert("Device OS: " + deviceOS); 
          alert("Device OS Version: " + deviceOSVersion);

尝试使用以下代码检查版本是否小于4,但无法正常工作?

if (deviceOSVersion < 4) {
    alert("android less than 4");
}

我已经尝试了几个东西,但没有运气。
非常感谢任何建议。

更新: 如果是Android版本小于4并且是iPhone,则尝试将最后的代码"useAnimations:animationen"设置为false,如果是iPhone,则"animationen"应该为true。 但是在iPhone上,下面的代码是false?

 var int = 0;
    var animationen='';

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

        function onDeviceReady()
        {
    getDeviceProperty();
        }

    function getDeviceProperty(){
     var deviceOS  = device.platform;  //fetch the device operating system
     var deviceOSVersion = device.version;  //fetch the device OS version
          alert("Device OS: " + deviceOS); 
          alert("Device OS Version: " + deviceOSVersion); 


if (deviceOS.indexOf('Android') >= 0) {
alert("its an android");
int = parseInt(deviceOSVersion);
if(int<4){
alert("animation false");
animationen=false;
}
alert("apptype = android");
}
else
{
alert("apptype = iphone");
animationen=true;
}                     
}


var jQT = new $.jQTouch({
useAnimations: animationen
});
2个回答

6
您需要使用parseInt对接收到的值进行转换,代码如下:
if( parseInt( deviceOSVersion, 10 ) < 4 )
{
    // your code
}

希望这能有所帮助。

实际上,您可以简单地进行字符串比较:deviceOSVersion < "4" - user276648
@user276648 不行,你必须把它解析为浮点数或整数(如果你不关心小数点或修订版本)。你也可以使用semver包,但这似乎已经足够了。 - spacesuitdiver

1
var int = 0;  
....// your code here
int = parseInt(deviceOSVersion);
if(int<4){
//do something
}

我已经检查过了,它可以正常工作。


谢谢大家。它可以工作,但我仍然有一个问题。我会更新我的代码并解释。 - Claes Gustavsson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接