如何在Android中检测平板电脑设备?

时间:2011-09-22 07:46:55

标签: android android-layout tablet

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

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

4 个答案:

答案 0 :(得分:18)

我认为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

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

考虑“新”的受保护目录(例如,值为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
GALAXY NOTE:400/615
NEXUS 7:961/528
GALAXY TAB(&gt; 7&amp;&amp;&lt; 10):1280/752
GALAXY S3:360/615

Wdp =宽度dp
Hdp =高度dp

答案 3 :(得分:0)

将此方法放在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英寸大小后开始使用平板电脑。