技术开发 频道

识别访问手机的能力

【IT168 技术文档】

概念
    我们通过以下代码可以获取手机的能力,从而针对不同能力的手机做出显示彩色还是黑白色的图片,显示的字体是否可以为彩色等等。
 
还可以进一步通过System.Web.Mobile.MobileCapabilities 的成员函数

System.Boolean HasCapability ( System.String delegateName , System.String optionalParameter )来查询手机是否支持某一特性,代码如下:

bool bHasWBMP =                
   ((MobileCapabilities)Request.Browser).HasCapability("PreferredImageMime", "image/vnd.wap.wbmp");
log.Info("Has WBMP?" + bHasWBMP.ToString());

还可以进一步通过System.Web.Mobile.MobileCapabilities 的成员函数


System.Boolean HasCapability ( System.String delegateName , System.String optionalParameter )来查询手机是否支持某一特性,代码如下:

bool bHasWBMP =                
   ((MobileCapabilities)Request.Browser).HasCapability("PreferredImageMime", "image/vnd.wap.wbmp");
log.Info("Has WBMP?" + bHasWBMP.ToString());
MobileCapabilities Codes
在asp.net页面中安排如下代码即可通过log4net打印出手机的MobileCapabilities:
代码: 
private void Page_Load(object sender, System.EventArgs e)
{  
    if(!IsPostBack)
    {
       log.Info("-============ 输出MobileCapabilities Begin ==============-");
       Type myType =(typeof(System.Web.Mobile.MobileCapabilities));
       System.Reflection.PropertyInfo[] myPropertyInfo =
       myType.GetProperties(System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.Instance);
       DisplayPropertyInfo(myType, myPropertyInfo);
       log.Info("-============ 输出MobileCapabilities End ==============-");
    }
。。。}

void DisplayPropertyInfo(Type t,System.Reflection.PropertyInfo[] myPropertyInfo)
{
    // Display information for all properties.

   System.Web.Mobile.MobileCapabilities m=( System.Web.Mobile.MobileCapabilities)Request.Browser;
   for(int i=0;i<myPropertyInfo.Length;i++)
  {
      Object result=new Object();
      System.Reflection.PropertyInfo myPropInfo = (System.Reflection.PropertyInfo)myPropertyInfo[i];
      try
      {
           result = t.InvokeMember (myPropInfo.Name,         System.Reflection.BindingFlags.GetProperty|System.Reflection.BindingFlags.GetField, null, m, new object [] {});
      }
      catch (System.MissingMethodException e)
     {
          result =e.Message;
     }
     log.Info ("[" + myPropInfo.Name+ " ]:<" + result+">");
  }
}

下面日志对应的手机型号是

Nokia7610/2.0 (4.0424.4ch) SymbianOS/7.0s Series60/2.1 Profile/MIDP-2.0 Configuration/CLDC-1.0
 输出的日志如下:
 
- -============ 输出MobileCapabilities Begin ==============-
 - [MobileDeviceManufacturer ]:<Nokia>
 - [MobileDeviceModel ]:<Unknown>
 - [GatewayVersion ]:<Nokia WAP Gateway 4.0/CD3/4.0.04)>
 - [GatewayMajorVersion ]:<4>
 - [GatewayMinorVersion ]:<0>
 - [PreferredRenderingType ]:<wml11>
 - [PreferredRenderingMime ]:<text/vnd.wap.wml>
 - [PreferredImageMime ]:<image/vnd.wap.wbmp>
 - [ScreenCharactersWidth ]:<20>
 - [ScreenCharactersHeight ]:<4>
 - [ScreenPixelsWidth ]:<90>
 - [ScreenPixelsHeight ]:<40>
 - [ScreenBitDepth ]:<1>
 - [IsColor ]:<False>
 - [InputType ]:<telephoneKeypad>
 - [NumberOfSoftkeys ]:<2>
 - [MaximumSoftkeyLabelLength ]:<5>
 - [CanInitiateVoiceCall ]:<True>
 - [CanSendMail ]:<False>
 - [HasBackButton ]:<False>
 - [RendersWmlDoAcceptsInline ]:<False>
 - [RendersWmlSelectsAsMenuCards ]:<False>
 - [RendersBreaksAfterWmlAnchor ]:<True>
 - [RendersBreaksAfterWmlInput ]:<True>
 - [RendersBreakBeforeWmlSelectAndInput ]:<False>
 - [RequiresPhoneNumbersAsPlainText ]:<True>
 - [RequiresUrlEncodedPostfieldValues ]:<False>
 - [RequiredMetaTagNameValue ]:<>
 - [RendersBreaksAfterHtmlLists ]:<True>
 - [RequiresUniqueHtmlInputNames ]:<False>
 - [RequiresUniqueHtmlCheckboxNames ]:<False>
 - [SupportsCss ]:<False>
 - [HidesRightAlignedMultiselectScrollbars ]:<False>
 - [IsMobileDevice ]:<True>
 - [RequiresAttributeColonSubstitution ]:<False>
 - [CanRenderOneventAndPrevElementsTogether ]:<False>
 - [CanRenderInputAndSelectElementsTogether ]:<True>
 - [CanRenderAfterInputOrSelectElement ]:<True>
 - [CanRenderPostBackCards ]:<False>
 - [CanRenderMixedSelects ]:<True>
 - [CanCombineFormsInDeck ]:<True>
 - [CanRenderSetvarZeroWithMultiSelectionList ]:<True>
 - [SupportsImageSubmit ]:<False>
 - [RequiresUniqueFilePathSuffix ]:<True>
 - [RequiresNoBreakInFormatting ]:<False>
 - [RequiresLeadingPageBreak ]:<False>
 - [SupportsSelectMultiple ]:<True>
 - [SupportsBold ]:<False>
 - [SupportsItalic ]:<False>
 - [SupportsFontSize ]:<False>
 - [SupportsFontName ]:<False>
 - [SupportsFontColor ]:<True>
 - [SupportsBodyColor ]:<True>
 - [SupportsDivAlign ]:<True>
 - [SupportsDivNoWrap ]:<False>
 - [RequiresContentTypeMetaTag ]:<False>
 - [RequiresDBCSCharacter ]:<False>
 - [RequiresHtmlAdaptiveErrorReporting ]:<False>
 - [RequiresOutputOptimization ]:<False>
 - [SupportsAccesskeyAttribute ]:<False>
 - [SupportsInputIStyle ]:<False>
 - [SupportsInputMode ]:<False>
 - [SupportsIModeSymbols ]:<False>
 - [SupportsJPhoneSymbols ]:<False>
 - [SupportsJPhoneMultiMediaAttributes ]:<False>
 - [MaximumRenderedPageSize ]:<1397>
 - [RequiresSpecialViewStateEncoding ]:<False>
 - [SupportsQueryStringInFormAction ]:<True>
 - [SupportsCacheControlMetaTag ]:<True>
 - [SupportsUncheck ]:<True>
 - [CanRenderEmptySelects ]:<True>
 - [SupportsRedirectWithCookie ]:<True>
 - [SupportsEmptyStringInCookieValue ]:<True>
 - [DefaultSubmitButtonLimit ]:<1>
 - [ClrVersion ]:<0.0>
 - [Type ]:<Unknown>
 - [Browser ]:<Nokia>
 - [Version ]:<0.0>
 - [MajorVersion ]:<0>
 - [MinorVersion ]:<0>
 - [Platform ]:<Unknown>
 - [TagWriter ]:<System.Web.UI.Html32TextWriter>
 - [EcmaScriptVersion ]:<0.0>
 - [MSDomVersion ]:<0.0>
 - [W3CDomVersion ]:<0.0>
 - [Beta ]:<False>
 - [Crawler ]:<False>
 - [AOL ]:<False>
 - [Win16 ]:<False>
 - [Win32 ]:<False>
 - [Frames ]:<False>
 - [Tables ]:<False>
 - [Cookies ]:<False>
 - [VBScript ]:<False>
 - [JavaScript ]:<False>
 - [JavaApplets ]:<False>
 - [ActiveXControls ]:<False>
 - [BackgroundSounds ]:<False>
 - [CDF ]:<False>
 
- -============ 输出MobileCapabilities End ==============-

0
相关文章