【问题标题】:How to detect a tablet device in Android?如何在 Android 中检测平板设备?
【发布时间】:2011-11-22 14:29:51
【问题描述】:

我正在尝试将我为智能手机开发的应用程序移植到稍作修改的平板电脑上。 Android中是否有API来检测设备是否是平板电脑?

我可以通过比较屏幕尺寸来做到这一点,但是检测平板电脑的正确方法是什么?

【问题讨论】:

    标签: android android-layout tablet


    【解决方案1】:

    我认为 API 中没有任何特定标志。

    基于 GDD 2011 示例应用程序,我将使用这些辅助方法:

    public static boolean isHoneycomb() {
        // Can use static final constants like HONEYCOMB, declared in later versions
        // of the OS since they are inlined at compile time. This is guaranteed behavior.
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
    }
    
    public static boolean isTablet(Context context) {
        return (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK)
                >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }
    
    public static boolean isHoneycombTablet(Context context) {
        return isHoneycomb() && isTablet(context);
    }
    

    Source

    【讨论】:

    • 谢谢山姆,我一定会试一试的。
    • Configuration.SCREENLAYOUT_SIZE_LARGE 定义至少 480x640 dp 的屏幕和至少 720x960 dp 的 XLARGE。 Galaxy Nexus 智能手机的分辨率为 1280x720 - 它不是平板电脑。有没有什么简单的方法来解决与屏幕宽度和高度相关的屏幕尺寸(例如 7 英寸或 10.1 英寸)?
    • DP 不等于屏幕分辨率。 Galaxy Nexus 的最小宽度为 360dp(纵向模式)。阅读:developer.android.com/guide/practices/screens_support.html
    【解决方案2】:

    如果分辨率(使用总像素阈值)建议,我会在应用程序设置中引入“平板电脑模式”,默认情况下启用。

    IFAIK Android 3.0 引入了真正的平板电脑支持,所有以前的版本都适用于手机,而平板电脑只是更大的手机 - 有一个;)

    【讨论】:

    • 什么是平板模式?它如何区分 Kindle Fire 和三星 Galaxy Note?
    【解决方案3】:

    考虑到“新”接受的目录(例如 values-sw600dp),我根据屏幕的宽度 DP 创建了这个方法:

    /**
     * Returns true if the current device is a smartphone or a "tabletphone"
     * like Samsung Galaxy Note or false if not.
     * A Smartphone is "a device with less than TABLET_MIN_DP_WEIGHT" dpi
     * 
     * @return true if the current device is a smartphone or false in other 
     * case
     */
    protected static boolean isSmartphone(Activity act){
        DisplayMetrics metrics = new DisplayMetrics();
        act.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    
        int dpi = 0;
        if (metrics.widthPixels < metrics.heightPixels){
            dpi = (int) (metrics.widthPixels / metrics.density);
        }
        else{
            dpi = (int) (metrics.heightPixels / metrics.density);
        }
    
        if (dpi < TABLET_MIN_DP_WEIGHT)         return true;
        else                                    return false;
    }
    
    
    
     public static final int TABLET_MIN_DP_WEIGHT = 450;  
    

    在此列表中,您可以找到一些流行设备和平板电脑尺寸的 DP:

    Wdp/Hdp

    GALAXY Nexus:360 / 567
    XOOM: 1280 / 752
    银河注:400 / 615
    NEXUS 7:961 / 528
    银河标签 (>7 && 影驰 S3:360 / 615

    Wdp = 宽度 dp
    Hdp = 高度 dp

    【讨论】:

    • 我注意到确定这是否有效,我只是建议为您的方法使用更好的措辞。一个名为 : isSmartphoneOrTablet 的方法以布尔值作为返回值,应该只在 google TV 或类似的东西上返回 false。
    【解决方案4】:

    将此方法放在onResume()中即可查看。

    public double tabletSize() {
    
         double size = 0;
            try {
    
                // Compute screen size
    
                DisplayMetrics dm = context.getResources().getDisplayMetrics();
    
                float screenWidth  = dm.widthPixels / dm.xdpi;
    
                float screenHeight = dm.heightPixels / dm.ydpi;
    
                size = Math.sqrt(Math.pow(screenWidth, 2) +
    
                                     Math.pow(screenHeight, 2));
    
            } catch(Throwable t) {
    
            }
    
            return size;
    
        }
    

    通常平板电脑在 6 英寸大小后开始。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      • 2013-02-02
      • 2016-02-04
      • 2015-08-23
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      相关资源
      最近更新 更多