2012-06-01 147 views
8

我确定我只是忽略了这个问题,但我似乎无法找到如何检查设备。
我想检查设备是横向模式的手机,平板电脑,纵向模式的平板电脑还是其他设备(计算机)。设备检查sencha touch 2

什么我是这样的:

if (Ext.platform.isPhone) { 
    console.log("phone"); 
} 

if (Ext.platform.isTablet) { 
    console.log("tablet"); 
} 

var x = Ext.platform; 

但平台是不确定的(可能是因为这是煎茶触摸1的方式)。

有没有人知道我访问设备检查的正确位置?

回答

18

Sencha Environment Detection答案通过简单的手段提供了一个大范围。

而不是Ext.os.is.Tablet,你可以通过Ext.os.deviceType将返回生活更轻松:

  • 电话
  • 平板
  • 桌面

注意:通过将“?deviceType =”添加到url中,该值也可以被欺骗。

http://localhost/mypage.html?deviceType=Tablet 

Ext.os.命名是单返回:

  • 的iOS
  • 的Android
  • 的webOS
  • 黑莓
  • RIMTablet
  • 的MacOS
  • 的Windows
  • Linux的
  • 巴达
  • 其他

浏览器检测的能力,通常可通过Ext.browser.name

东西我是最近才遇到的,我爱的是特征检测 - 允许编码类似的Modernizr/YepNope设备的基于断能力。 Ext.feature报价:

  • Ext.feature.has.Audio
  • Ext.feature.has.Canvas
  • Ext.feature.has.ClassList
  • Ext.feature.has.CreateContextualFragment
  • Ext.feature.has.Css3dTransforms
  • Ext.feature.has.CssAnimations
  • Ext.feature.has.CssTransforms
  • Ext.feature.has.CssTransitions
  • Ext.feature.has.DeviceMotion
  • Ext.feature.has.Geolocation
  • Ext.feature.has.History
  • Ext.feature.has.Orientation
  • Ext.feature.has.OrientationChange
  • Ext.feature.has.Range
  • Ext.feature.has.SqlDatabase
  • Ext.feature.has.Svg
  • Ext.feature.has.Touch
  • Ext.feature.has.Video
  • Ext.feature.has.Vml
  • Ext.feature.has.WebSockets

为了检测全屏/应用在iOS /主屏幕的浏览模式:

window.navigator.standalone == true 

方向Ext.device.Orientation和方向的变化:

Ext.device.Orientation.on({ 
    scope: this, 
    orientationchange: function(e) { 
     console.log('Alpha: ', e.alpha); 
     console.log('Beta: ', e.beta); 
     console.log('Gamma: ', e.gamma); 
    } 
}); 

方向是基于视口。我通常添加一个更可靠的监听器:

onOrientationChange: function(viewport, orientation, width, height) { 
     // test trigger and values 
     console.log('o:' + orientation + ' w:' + width + ' h:' + height); 

     if (width > height) { 
      orientation = 'landscape'; 
     } else { 
      orientation = 'portrait'; 
     } 
     // add handlers here... 
    } 
4

使用Ext.env.OS'sis()方法。

请注意,只有主要成分,版本简化价值 通过直接属性检验可用。支持的值是:iOS上, 的iPad,iPhone,iPod的,Android,WebOS的,黑莓,巴达,MacOS的,Windows中, Linux和其他

if (Ext.os.is('Android')) { ... } 
if (Ext.os.is.Android2) { ... } // Equivalent to (Ext.os.is.Android && Ext.os.version.equals(2)) 

if (Ext.os.is.iOS32) { ... } // Equivalent to (Ext.os.is.iOS && Ext.os.version.equals(3.2)) 

另外,您还可以使用PhoneGap Device API

+0

我确实看到过这种方式,但问题在于我还想指定设备是移动设备还是平板电脑。 Android例如可以在平板电脑或移动设备上运行。 –

2

我找到了答案:

什么似乎是这样的Ext.os.is.(平板电脑/电话或其他)是真实的或未定义的。如果不是这种情况,它将是未定义的。 I.a.在平板电脑上时为Ext.os.is.Tablet,在未定义时为undefined。

所以这就是我一直在寻找

if(Ext.os.is.Tablet){ 
     this._bIsTablet = true; 
    }else if(Ext.os.is.Phone){ 
     this._bIsPhone = true; 
    }else{ 
     this._bIsOther = true; 
    }