确定设备是智能手机还是平板电脑? [英] Determine if the device is a smartphone or tablet?

查看:38
本文介绍了确定设备是智能手机还是平板电脑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取有关设备的信息,以确定它是智能手机还是平板电脑.我该怎么做?

I would like to get info about a device to see if it's a smartphone or tablet. How can I do it?

我想根据设备类型显示来自资源的不同网页:

I would like to show different web pages from resources based on the type of device:

String s="Debug-infos:";
s += "
 OS Version: " + System.getProperty("os.version") + "(" +    android.os.Build.VERSION.INCREMENTAL + ")";
s += "
 OS API Level: " + android.os.Build.VERSION.SDK;
s += "
 Device: " + android.os.Build.DEVICE;
s += "
 Model (and Product): " + android.os.Build.MODEL + " ("+ android.os.Build.PRODUCT + ")";

但是,对于我的情况,这似乎没用.

However, it seems useless for my case.

这个解决方案现在对我有用:

This solution works for me now:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;

if (SharedCode.width > 1023 || SharedCode.height > 1023){
   //code for big screen (like tablet)
}else{
   //code for small screen (like smartphone)
}

推荐答案

Android 培训中讨论了此主题:

This subject is discussed in the Android Training:

使用最小宽度限定符

如果您阅读了整个主题,它们会解释如何在特定值文件(如 res/values-sw600dp/attrs.xml)中设置布尔值:

If you read the entire topic, they explain how to set a boolean value in a specific value file (as res/values-sw600dp/attrs.xml):

<resources>
    <bool name="isTablet">true</bool>
</resources>

因为 sw600dp 限定符仅对 android 3.2 以上的平台有效.如果您想确保此技术适用于所有平台(3.2 之前),请在 res/values-xlarge 文件夹中创建相同的文件:

Because the sw600dp qualifier is only valid for platforms above android 3.2. If you want to make sure this technique works on all platforms (before 3.2), create the same file in res/values-xlarge folder:

<resources>
    <bool name="isTablet">true</bool>
</resources>

然后,在标准"值文件(如 res/values/attrs.xml)中,将布尔值设置为 false:

Then, in the "standard" value file (as res/values/attrs.xml), you set the boolean to false:

<resources>
    <bool name="isTablet">false</bool>
</resources>

然后在您的活动中,您可以获得此值并检查您是否在平板电脑大小的设备上运行:

Then in you activity, you can get this value and check if you are running in a tablet size device:

boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize) {
    // do something
} else {
    // do something else
}

这篇关于确定设备是智能手机还是平板电脑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆