2014-01-21 99 views
0

在我的应用程序中,用户界面根据手机或平板电脑而改变。所以我使用了2类手机和平板电脑。检测手机和平板电脑不工作的代码

它在模拟器中工作正常。但在真正的平板电脑设备中,它不能工作,需要移动设备级别。

我的代码来检测设备:

public boolean isTablet(Context context) 
{ 
    boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4); 
    boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE); 
    return (xlarge || large);  
} 

请告诉我我的代码错在这里。我只能在真实设备上获得移动版式。

回答

0

检查Android开发中的哪种设备非常不准确。试试我的代码

public static boolean isTabletDevice(Context activityContext) { 
    // Verifies if the Generalized Size of the device is XLARGE to be 
    // considered a Tablet 
    boolean xlarge = ((activityContext.getResources().getConfiguration().screenLayout & 
      Configuration.SCREENLAYOUT_SIZE_MASK) == 
      Configuration.SCREENLAYOUT_SIZE_XLARGE); 

    // If XLarge, checks if the Generalized Density is at least MDPI 
    // (160dpi) 
    if (xlarge) { 
     DisplayMetrics metrics = new DisplayMetrics(); 
     Activity activity = (Activity) activityContext; 
     activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); 

     // MDPI=160, DEFAULT=160, DENSITY_HIGH=240, DENSITY_MEDIUM=160, 
     // DENSITY_TV=213, DENSITY_XHIGH=320 
     if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT 
       || metrics.densityDpi == DisplayMetrics.DENSITY_HIGH 
       || metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM 
       || metrics.densityDpi == DisplayMetrics.DENSITY_TV 
       || metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) { 

      // Yes, this is a tablet! 
      return true; 
     } 
    } 

    // No, this is not a tablet! 
    return false; 
} 
+0

它不适合我 – user2054192