【问题标题】:How to detect 7" Android tablet in code如何在代码中检测 7" Android 平板电脑
【发布时间】:2012-05-23 16:43:25
【问题描述】:

我试图在我的代码中检测 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;
    }

【问题讨论】:

  • 您编辑的代码将声称 Galaxy Nexus 是平板电脑。
  • @CommonsWare 我已经测试了代码并且可以明确地说 Galaxy Nexus 不会评估为 True。您的测试能否证明并非如此?
  • 抱歉,我是根据您上面的评论说 Galaxy Nexus 是 1024x600(实际上是 1280x720)。不过,更重要的是,您只是在寻找 1024x600 显示器,它与尺寸无关。它恰好与某些 7" 平板电脑匹配。它可能与运行其他分辨率的其他 7" 平板电脑不匹配。它可能与运行该分辨率的 xhdpi 手机匹配。
  • @CommonsWare 如果您使用来自 Google Play 的应用程序 - ScreenInfo,您可以查看有关设备尺寸的所有信息。我的告诉我我的 Galaxy Nexus 是 720px x 1184px 和 592dp x 360dp。所以很明显它不会被我上面的代码检测到。你有什么解决方案来充分检测 7 英寸平板电脑?我认为 Justin 的解决方案是一个很好的解决方案。
  • ScreenInfo 正在减去一些东西(状态或系统栏),因为设备是 1280x720。 Justin 的第一个代码清单很好;第二个代码清单有效,尽管我的纯粹主义者不会使用pow()(乘法有机会更快)或sqrt()(只需与值的平方进行比较)。

标签: android kindle-fire android-screen


【解决方案1】:

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

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

float widthInInches = metrics.widthPixels / metrics.xdpi;
float heightInInches = metrics.heightPixels / metrics.ydpi;

然后可以计算大小

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; 

根据上面的 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; 

【讨论】:

  • 好的,在 Galaxy Nexus 上 widthInInches 变为 2.2834647 而 heightInInches 为 3.714567。对于 Kindle fire,它们是 3.644808 和 6.1816273。似乎是一个很好的解决方案。谢谢。
  • 听起来不错。使用勾股定理计算对角线,我们得到大约 7.17 英寸的 kindle 火焰。如果您使用半英寸错误缓冲区(6.5" 到 7.5"),您应该能够轻松获得所有 7" 平板电脑。
  • 您还应该考虑到 sizeInInches 会根据屏幕方向返回略有不同的数字。例如,Asus Transformer Pad TF300 返回以下测量值:9.18(纵向)、9.27(横向)。三星 GN 返回:4.36(港口)4.41(陆地)。
  • xdpi 和 ydpi 不能被制造商正确设置 - 有时它们根本没有设置,其他时候(Samsung Galaxy Tab 1)它们会谎报分辨率。
【解决方案2】:

如果您关心的是平板电脑而不是尺寸,具体来说:有 Build 类,它可以告诉您型号和产品等信息。

【讨论】:

  • 谢谢,但我特别关心平板电脑和手机之间的区别。
  • 编辑:那里有太多的平板电脑和手机版本,更不用说即将推出的新“平板手机”了。
【解决方案3】:

请参阅以下内容。似乎不适用于所有设备。如果这两款平板电脑是您唯一担心的设备,我会尝试任何一种实现。

https://stackoverflow.com/a/10080537/300972

https://groups.google.com/group/android-developers/browse_thread/thread/cae5ff90157098b1?pli=1

【讨论】:

  • 第一个链接中的解决方案不如本文中的解决方案正确。
【解决方案4】:

我知道这是旧线程,但是这种方式呢:

public boolean isLargeScreen() {

    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.device_screen, null);
    FrameLayout menu = (FrameLayout) view.findViewById(R.id.menu);


    if (left_menu != null) {
        return true;
    } else {
        return false;
    }
}

然后:

if ( isLargeScreen() ) {
    // do something for large screen & extra large screen
} else {
    // do something for normal screen size
}

和xml布局(例如):

res/layout/device_screen.xml // 正常屏幕尺寸的布局(“默认”)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    [leave it blank]

</LinearLayout>

res/layout-large/device_screen.xml // 大屏幕平板7'的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/menu"
        ... />

</LinearLayout>

res/layout-xlarge/device_screen.xml // 超大屏幕布局 > tablet 10'

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/menu"
        ... />

</LinearLayout>

在 HTC Desire 和三星 Galaxy Note 10.1 上测试 - 效果很好!

【讨论】:

  • 最早可以使用的 SDK 目标是什么?我需要我的应用程序运行到目标 4。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多