如何在代码中检测7"Android平板电脑

Igo*_*sky 2 android android-screen kindle-fire

我试图在我的代码中检测7"平板电脑(即Kindle Fire和Nook Color).但是,仅测试1024x600的最小尺寸并不好,因为那时Galaxy Nexus也会通过这个测试.任何人都有检测这个的经验那种信息?

谢谢,伊戈尔

编辑: 我找到了一种方法来检测Kindle Fire和Nook Color设备,代码如下:

Activity activity = (Activity) activityContext;
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay().getHeight();
if ((width == 1024 && height == 600) || (width == 600 && height == 1024)) {    
        //Detects 7" tablets: i.e. Kindle Fire & Nook Color
        isTablet = true;
    }
Run Code Online (Sandbox Code Playgroud)

Jus*_*ler 5

要计算设备的高度和宽度(以英寸为单位),您可以使用以下代码.

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

float widthInInches = metrics.widthPixels / metrics.xdpi;
float heightInInches = metrics.heightPixels / metrics.ydpi;
Run Code Online (Sandbox Code Playgroud)

然后可以计算大小

double sizeInInches = Math.sqrt(Math.pow(widthInInches, 2) + Math.pow(heightInInches, 2));
//0.5" buffer for 7" devices
boolean is7inchTablet = sizeInInches >= 6.5 && sizeInInches <= 7.5; 
Run Code Online (Sandbox Code Playgroud)

根据上面的Commonsware建议,可能更快,但不太明显的实现.

double sizeInInchesSquared = (widthInInches * widthInInches) + (heightInInches * heightInInches);
//0.5" buffer for 7" devices (6.5^2 = 42.25) (7.5^2 = 56.25)
boolean is7inchTablet = sizeInInchesSquared >= 42.25 && sizeInInchesSquared <= 56.25; 
Run Code Online (Sandbox Code Playgroud)