平板电脑或手机 - Android
有没有办法检查用户是否使用平板电脑或手机? 我的倾斜功能和新平板电脑(Transformer)出现问题
Is there a way to check if the user is using a tablet or a phone?
I've got problems with my tilt function and my new tablet (Transformer)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
正如前面所提到的,你不想检查设备是平板电脑还是手机,而是想了解设备的功能,
大多数时候,平板电脑和手机的区别在于屏幕size 这就是为什么你想使用不同的布局文件。这些文件存储在
res/layout-
目录中。您可以在res/values-
目录中为每个布局创建一个 XML 文件,并将 int/bool/string 资源放入其中以区分您使用的布局。示例:
文件
res/values/screen.xml
(假设res/layout/
包含手机的布局文件)文件
res/values-sw600dp/screen.xml
(假设res/layout-sw600dp/
包含 Nexus 7 等小型平板电脑的布局文件)文件
res/values-sw720dp/screen.xml
(假设res/layout-sw720dp/
包含 Nexus 10 等大型平板电脑的布局文件): 屏幕类型可通过 R.string.screen_type 常量访问。
As it has been mentioned before, you do not want to check whether the device is a tablet or a phone but you want to know about the features of the device,
Most of the time, the difference between a tablet and a phone is the screen size which is why you want to use different layout files. These files are stored in the
res/layout-<qualifiers>
directories. You can create an XML file in the directoyres/values-<same qualifiers>
for each of your layouts and put an int/bool/string resource into it to distinguish between the layouts you use.Example:
File
res/values/screen.xml
(assumingres/layout/
contains your layout files for handsets)File
res/values-sw600dp/screen.xml
(assumingres/layout-sw600dp/
contains your layout files for small tablets like the Nexus 7)File
res/values-sw720dp/screen.xml
(assumingres/layout-sw720dp/
contains your layout files for large tablets like the Nexus 10):Now the screen type is accessible via the
R.string.screen_type
constant.要检测设备是否是平板电脑,请使用以下代码:
LARGE 和 XLARGE 屏幕尺寸由制造商根据使用时距眼睛的距离确定(因此是平板电脑的概念)。
详细信息:http://groups.google.com/group/android-开发人员/browse_thread/thread/d6323d81f226f93f
To detect whether or not the device is a tablet use the following code:
LARGE and XLARGE Screen Sizes are determined by the manufacturer based on the distance from the eye they are to be used at (thus the idea of a tablet).
More info : http://groups.google.com/group/android-developers/browse_thread/thread/d6323d81f226f93f
这篇文章对我帮助很大,
不幸的是我没有必要的声誉来评估所有对我有帮助的答案。
我需要确定我的设备是平板电脑还是手机,这样我就能够实现屏幕的逻辑。根据我的分析,从 MDPI 开始,平板电脑的尺寸必须超过 7 英寸(超大)。
下面是根据这篇文章创建的代码。
This post helped me a lot,
Unfortunately I don't have the reputation necessary to evaluate all the answers that helped me.
I needed to identify if my device was a tablet or a phone, with that I would be able to implement the logic of the screen. And in my analysis the tablet must be more than 7 inches (Xlarge) starting at MDPI.
Here's the code below, which was created based on this post.
为什么不计算屏幕对角线的大小并用它来决定该设备是手机还是平板电脑?
当然,人们可能会争论阈值是否应该为 9 英寸或更小。
Why not calculate the size of the screen diagonal and use that to make the decision whether the device is a phone or tablet?
Of course one can argue whether the threshold should be 9 inches or less.
没有什么区别。您应该定义您认为的差异并进行检查。 Galaxy Tab 是手机吗?或者平板电脑?为什么?
您应该定义您正在寻找哪些特定功能,并为其编写代码。
看来您正在寻找“倾斜”。我认为这与加速度计相同(这是一个词吗?)。您可以使用以下命令检查设备是否支持它:(
来自 http:// stuffthathappens.com/blog/2009/03/15/android-accelerometer/ 我没有测试过)
there is no difference. You should define what you think is the difference, and check for that. Is a galaxy tab a phone? or a tablet? and why?
You should define what specific features you are looking for, and code for that.
It seems you are looking for 'tilt'. I think this is the same as the accelerometer (is that a word?). You can just check if the device supports it, using:
(from http://stuffthathappens.com/blog/2009/03/15/android-accelerometer/ . i have not tested it)
我的假设是,当您定义“手机/电话”时,您希望知道是否可以在设备上拨打电话,而这在定义为“平板电脑”的设备上无法完成。验证这一点的方法如下。如果您想了解基于传感器、屏幕尺寸等的信息,那么这确实是一个不同的问题。
此外,虽然使用屏幕分辨率或资源管理大与超大,可能是过去新的“移动”设备的有效方法,但现在配备了如此大的屏幕和高分辨率,以至于它们模糊了这条界限,而如果您真的愿意的话要了解电话呼叫与没有电话呼叫功能,以下是“最佳”。
My assumption is that when you define 'Mobile/Phone' you wish to know whether you can make a phone call on the device which cannot be done on something that would be defined as a 'Tablet'. The way to verify this is below. If you wish to know something based on sensors, screen size, etc then this is really a different question.
Also, while using screen resolution, or the resource managements large vs xlarge, may have been a valid approach in the past new 'Mobile' devices are now coming with such large screens and high resolutions that they are blurring this line while if you really wish to know phone call vs no phone call capability the below is 'best'.
在 Google IOSched 2017 应用程序中 源码,使用的方法如下:
In the Google IOSched 2017 app source code, the following method is used:
基于 Robert Dale Johnson III 和 Helton Isac 我想出了这段代码希望这很有用
所以在你的代码中做一个过滤器
Based on Robert Dale Johnson III and Helton Isac I came up with this code Hope this is useful
So in your code make a filter like
不需要代码
其他答案列出了以编程方式确定设备是手机还是平板电脑的多种方法。但是,如果您阅读文档,这不是推荐的方式支持各种屏幕尺寸。
相反,为平板电脑或手机声明不同的资源。您可以通过为
layout
、values
等添加其他资源文件夹来执行此操作。对于 Android 3.2(API 级别 13),添加
sw600dp 文件夹。这意味着最小宽度至少为 600dp,大约是手机/平板电脑的差距。但是,您也可以添加其他尺寸。查看此答案,了解如何添加其他布局资源文件的示例。
如果您还支持 Android 3.2 之前的设备,则需要添加
large
或xlarge
文件夹以支持平板电脑。 (手机通常小
和普通
。)下面是一张图片,显示了在为不同的屏幕尺寸添加额外的 xml 文件后您的资源可能会是什么样子。
使用此方法时,系统会为您确定一切。您不必担心运行时正在使用哪个设备。您只需提供适当的资源,然后让 Android 完成所有工作。
注释
值得一读的 Android 文档
No code needed
The other answers list many ways of programmatically determining whether the device is a phone or tablet. However, if you read the documentation, that is not the recommended way to support various screen sizes.
Instead, declare different resources for tablets or phones. You do this my adding additional resource folders for
layout
,values
, etc.For Android 3.2 (API level 13) on, add a
sw600dp
folder. This means the smallest width is at least 600dp, which is approximately the phone/tablet divide. However, you can also add other sizes as well. Check out this answer for an example of how to add an additional layout resource file.If you are also supporting pre Android 3.2 devices, then you will need to add
large
orxlarge
folders to support tablets. (Phones are generallysmall
andnormal
.)Here is an image of what your resources might like after adding an extra xml files for different screen sizes.
When using this method, the system determines everything for you. You don't have to worry about which device is being used at run time. You just provide the appropriate resources and let Android do all the work.
Notes
Android docs worth reading
对于那些想参考Google决定哪些设备将使用平板电脑UI的代码可以参考以下内容:
For those who want to refer to Google's code of deciding which devices will use a Tablet UI can refer to below:
这个方法是Google推荐的。我在 Google 官方 Android 应用
iosched
中看到此代码This method is a recommend by Google. I see this code in Google Offical Android App
iosched
如果屏幕尺寸检测在较新的设备上未返回正确的值,请尝试:
在 Android 4.2.2 上测试(抱歉我的英语不好。)
If screen size detection doesn't return correct value on newer devices, give a try:
Tested on Android 4.2.2 (sorry for my English.)
如果您的目标只是 API 级别 >= 13,那么请尝试
欢呼:-)
If you are only targeting API level >= 13 then try
cheers :-)
考虑到“新”接受的目录(例如 value-sw600dp),我根据屏幕宽度 DP 创建了此方法:
在此列表中,您可以找到一些流行设备和平板电脑尺寸的 DP:
Wdp / Hdp
GALAXY Nexus : 360 / 567
XOOM:1280 / 752
银河笔记:400 / 615
Nexus 7:961 / 528
银河选项卡 (>7 &<10):1280 / 752
GALAXY S3:360 / 615
Wdp = 宽度 dp
Hdp = 高度 dp
Thinking on the "new" acepted directories (values-sw600dp for example) i created this method based on the screen' width DP:
And on this list you can find some of the DP of popular devices and tablet sizes:
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 = Width dp
Hdp = Height dp
好吧,对我有用的最佳解决方案非常简单:
像这样使用:
我真的不想查看像素大小,而只想依赖屏幕大小。
Nexus 7 (LARGE) 被检测为平板电脑,但 Galaxy S3 (NORMAL) 则无法正常工作。
Well, the best solution that worked for me is quite simple:
Used like this:
I really don't want to look at the pixels sizes but only rely on the screen size.
Works well as Nexus 7 (LARGE) is detected as a tablet, but not Galaxy S3 (NORMAL).
当设备是平板电脑时,使用此方法会返回 true
Use this method which returns true when the device is a tablet
我知道这不是您问题的直接答案,但这里的其他答案很好地说明了如何识别屏幕尺寸。您在问题中写道,您遇到了倾斜问题,这也发生在我身上。
如果您在智能手机上运行陀螺仪(或旋转传感器),则根据该设备的默认方向,x 轴和 y 轴的定义可能与平板电脑上不同(例如,三星 GS2 是默认纵向,三星 GT-7310 是默认纵向)默认横向,新的 Google Nexus 7 默认纵向,尽管它是平板电脑!)。
现在,如果您想使用陀螺仪,您最终可能会得到一个适用于智能手机的可行解决方案,但在某些平板电脑上会出现轴混乱,反之亦然。
如果您使用上面的解决方案之一仅选择屏幕尺寸,然后应用
翻转轴(如果它具有大或超大屏幕尺寸),这可能适用于 90% 的情况,但例如在 Nexus 7 上会造成麻烦(因为它有默认的纵向方向和大屏幕尺寸)。
解决此问题的最简单方法是在 API 演示附带的 RotationVectorSample 中提供,方法是在清单中将 sceenOrientation 设置为
nosensor
:I know this is not directly an answer to your question, but other answers here give a good idea of how to identify screen size. You wrote in your question that you got problems with the tilting and this just happened to me as well.
If you run the gyroscope (or rotation sensor) on a smartphone the x- and y-axis can be differently defined than on a tablet, according to the default orientation of that device (e.g. Samsung GS2 is default portrait, Samsung GT-7310 is default landscape, new Google Nexus 7 is default portrait, although it is a tablet!).
Now if you want to use Gyroscope you might end up with a working solution for smartphones, but axis-confusion on some tablets or the other way round.
If you use one of the solutions from above to only go for screen-size and then apply
to flip the axis if it has a large or xlarge screen-size this might work in 90% of the cases but for example on the Nexus 7 it will cause troubles (because it has default portrait orientation and a large screen-size).
The simplest way to fix this is provided in the RotationVectorSample that ships with the API demos by setting the sceenOrientation to
nosensor
in your manifest:包管理器中的 com.sec.feature.multiwindow.tablet 仅特定于平板电脑,而 com.sec.feature.multiwindow.phone 特定于手机。
com.sec.feature.multiwindow.tablet in package manager is specific only to tablet and com.sec.feature.multiwindow.phone is specific to phone.
下面的方法是计算设备屏幕的对角线长度来确定设备是手机还是平板电脑。此方法唯一关心的是决定设备是否为平板电脑的阈值是多少。在下面的示例中,我将其设置为 7 英寸及以上。
below method is calculating the device screen's diagonal length to decide the device is a phone or tablet. the only concern for this method is what is the threshold value to decide weather the device is tablet or not. in below example i set it as 7 inch and above.
区分手机和平板电脑变得越来越困难。例如(截至 2015 年 8 月)Samsung Mega 6.3 设备从 sw600dp 提取资源文件夹——就 Android 而言,它是一台平板电脑。
@Vyshnavi 的答案适用于我们测试过的所有设备,但不适用于 Mega 6.3。
@Helton Isac 上面的答案将 Mega 6.3 作为手机返回 - 但由于该设备仍然从 sw600dp 获取资源,因此可能会导致其他问题 - 例如,如果您在手机上使用 viewpager 而不是在平板电脑上使用,您最终会遇到 NPE 错误。
最后,似乎有太多的条件需要检查,我们可能不得不接受有些手机实际上是平板电脑:-P
It is getting increasingly harder to draw the line between phone and tablet. For instance (as of Aug 2015) the Samsung Mega 6.3 device pulls resources from the sw600dp folders -- so as far as Android is concerned it is a tablet.
The answer from @Vyshnavi works in all devices we have tested but not for Mega 6.3.
@Helton Isac answer above returns the Mega 6.3 as a phone -- but since the device still grabs resources from sw600dp it can cause other issues - for instance if you use a viewpager for phones and not for tablets you'll wind up with NPE errors.
In the end it just seems that there are too many conditions to check for and we may just have to accept that some phones are actually tablets :-P
这是我使用的方法:
使用:
Configuration。SCREENLAYOUT_SIZE_MASK
配置。SCREENLAYOUT_SIZE_LARGE
这是推荐的方法!
This is the method that i use :
Using:
Configuration.SCREENLAYOUT_SIZE_MASK
Configuration.SCREENLAYOUT_SIZE_LARGE
This is the recommended method!
为什么用这个?
我在上面看到了很多方法。配置类在下面得到了正确的答案:
只需调用:
可以吗?
why use this?
i see many ways above.the Configuration class has get the right answer just below:
just call :
it's ok?
我只需要在布局文件中检测智能手机/平板电脑,因为我正在使用导航代码。
我首先做的是创建一个layout-sw600dp目录,但效果不佳,因为它会在我的诺基亚8上以横向模式激活,但屏幕高度太小。
所以,我将目录重命名为layout-sw600dp-h400dp,然后就得到了想要的效果。 h-xxxdp 参数应取决于您想要在布局上放置的内容量,因此应取决于应用程序。
I needed to detect the smartphone/tablet only in the layout file, because I'm using the navigation code.
What I did first was to create a layout-sw600dp directory but it was not working well because it would activate on my Nokia 8 in landscape mode, but the screen height would be too small.
So, I renamed the directory as layout-sw600dp-h400dp and then I got the desired effect. The h-xxxdp parameter should depend on how much content you want to drop on your layout and as such should be application dependent.
请查看下面的代码。
Please check out below code.
我认为平板电脑的宽度和高度的最小和最大 600 像素,
所以需要知道屏幕密度和以 dp 为单位的高度/宽度,
检索值:
I think a tablet has a min and max 600 px width and height,
so need to know the screen density and the height/width in dp,
to retrieve the value :
例如,手机和平板电脑之间有一个重要的区别(至少对于我的程序而言)。这是设备的默认方向。手机为纵向,平板电脑为横向。以及分别确定设备的方法:
编辑:
与 Dan Hulme 讨论后更改了该方法的名称。
E.g. have one important difference (at least for my program) between the phone and tablet. It is the default orientation of the device. Phone has a portrait orientation, the tablet - landscape. And respectively method to determine the device:
EDITED:
Following the discussions with Dan Hulme changed the name of the method.
我推荐 android 库“caffeine”,它包含手机或平板电脑,以及 10 英寸~!
非常容易使用。
图书馆在这里。
https://github.com/ShakeJ/Android-Caffeine-library
并使用
I'm recommend android library 'caffeine' That's contain get Phone or tablet, and 10inch~!
very easy use.
the library is here.
https://github.com/ShakeJ/Android-Caffeine-library
and use
对我来说,手机和平板电脑之间的区别不是设备的尺寸和/或像素密度,这会随着技术和品味而变化,而是屏幕比例。如果我要显示文本屏幕,我需要知道是否需要 15 行长行(手机)和 20 行短行(平板电脑)。对于我的游戏,我使用以下内容对我的软件将处理的矩形进行大致估计:
For me the distinction between phone and tablet is not size of device and/or pixel density, which will change with technology and taste, but rather the screen ratio. If I'm displaying a screen of text I need to know if, say, 15 long lines are needed (phone) vs 20 shorter lines (tablet). For my game I use the following for a ballpark estimation of the rectangle my software will be dealing with:
我认为这是最简单的诚实方式。这将检查正在使用的屏幕尺寸:
祝你好运!
I think this is the easiest way to be honest. This will check the screen size that's being used:
Best of luck!