簡體   English   中英

如何在Android中檢測平板電腦設備?

[英]How to detect a tablet device in Android?

我試圖將我為智能手機開發的應用程序移植到平板電腦上進行微小修改。 Android中是否有API來檢測設備是否為平板電腦?

我可以通過比較屏幕尺寸來做到這一點,但檢測平板電腦的正確方法是什么?

我不認為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);
}

資源

考慮“新”的acepted目錄(例如值 - 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(> 7 && <10):1280/752
GALAXY S3:360/615

Wdp =寬度dp
Hdp =高度dp

我將在應用程序設置中引入“平板電腦模式”,如果分辨率(使用總像素閾值)表明它,默認情況下將啟用。

IFAIK Android 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英寸大小后開始。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM