use*_*635 251 android device-detection
我想获得有关设备的信息,看看它是智能手机还是平板电脑.我该怎么做?
我想根据设备类型显示来自资源的不同网页:
String s="Debug-infos:";
s += "\n OS Version: " + System.getProperty("os.version") + "(" + android.os.Build.VERSION.INCREMENTAL + ")";
s += "\n OS API Level: " + android.os.Build.VERSION.SDK;
s += "\n Device: " + android.os.Build.DEVICE;
s += "\n Model (and Product): " + android.os.Build.MODEL + " ("+ android.os.Build.PRODUCT + ")";
Run Code Online (Sandbox Code Playgroud)
但是,对我来说似乎没用.
这个解决方案现在适用于我:
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)
}
Run Code Online (Sandbox Code Playgroud)
ol_*_*_er 733
Android培训中讨论了此主题:
如果您阅读整个主题,他们将解释如何在特定值文件中设置布尔值(如res/values-sw600dp/attr.xml):
<resources>
<bool name="isTablet">true</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)
因为sw600dp限定符仅对android 3.2以上的平台有效.如果要确保此技术适用于所有平台(3.2之前),请在res/values-xlarge文件夹中创建相同的文件:
<resources>
<bool name="isTablet">true</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)
然后,在"标准"值文件(作为res/values /)中,将布尔值设置为false:
<resources>
<bool name="isTablet">false</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)
然后在您的活动中,您可以获取此值并检查您是否在平板电脑尺寸设备中运行:
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize) {
// do something
} else {
// do something else
}
Run Code Online (Sandbox Code Playgroud)
gts*_*ouk 78
我认为平板电脑至少有6.5英寸的屏幕.这是基于Nolf上面的答案计算它的方法.
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
float yInches= metrics.heightPixels/metrics.ydpi;
float xInches= metrics.widthPixels/metrics.xdpi;
double diagonalInches = Math.sqrt(xInches*xInches + yInches*yInches);
if (diagonalInches>=6.5){
// 6.5inch device or bigger
}else{
// smaller device
}
Run Code Online (Sandbox Code Playgroud)
Rob*_*III 40
我的假设是,当您定义"移动/电话"时,您希望知道您是否可以在设备上拨打电话,而这些电话无法用于定义为"平板电脑"的内容.验证这一点的方法如下.如果你想知道基于传感器,屏幕尺寸等的东西,那么这实际上是一个不同的问题.
此外,虽然使用屏幕分辨率,或资源管理大与xlarge,可能是一个有效的方法在过去新的'移动'设备现在有这么大的屏幕和高分辨率,他们正在模糊这一行,如果你真的希望要知道电话呼叫与没有电话呼叫功能,以下是"最佳".
TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if(manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE){
return "Tablet";
}else{
return "Mobile";
}
Run Code Online (Sandbox Code Playgroud)
Jas*_*son 30
我喜欢Ol_v_er的解决方案而且简单但是,我发现它并不总是那么简单,新设备和显示器不断出现的东西,我希望在尝试找出实际的屏幕尺寸时更加"细化" .我在这里找到的另一个解决方案是使用String资源而不是布尔值来指定平板电脑大小.因此,不要只将true放在/res/values-sw600dp/screen.xml文件中(假设您的布局适用于小型平板电脑),您可以:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="screen_type">7-inch-tablet</string>
</resources>
Run Code Online (Sandbox Code Playgroud)
参考如下,然后根据结果做你需要的:
String screenType = getResources().getString(R.string.screen_type);
if (screenType.equals("7-inch-tablet")) {
// do something
} else {
// do something else
}
Run Code Online (Sandbox Code Playgroud)
Sean O'Toole以编程方式检测7英寸和10英寸平板电脑的答案也是我一直在寻找的.如果这里的答案不允许您按照您的意愿具体说明,您可能需要检查一下.他在解释如何计算不同的指标以确定您实际处理的内容方面做得很好.
UPDATE
在查看Google I/O 2013应用程序源代码时,我遇到了以下用于识别设备是否是平板电脑的内容,因此我想我会添加它.上面给你一点"控制",但如果你只想知道它是否是平板电脑,下面的内容非常简单:
public static boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
Run Code Online (Sandbox Code Playgroud)
小智 7
由于平板电脑通常比智能手机更大,并且由于低分辨率平板电脑可能具有与高分辨率智能手机相同的像素数,因此解决此问题的一种方法是计算设备的物理尺寸(不是分辨率):
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float yInches= metrics.heightPixels/metrics.ydpi;
float xInches= metrics.widthPixels/metrics.xdpi;
if (yInches> smallestTabletSize|| xInches > smallestTabletSize)
{
//We are on a
}
Run Code Online (Sandbox Code Playgroud)
我在所有应用程序中都使用了这种方法,并且可以成功运行:
public static boolean isTablet(Context ctx){
return (ctx.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
Run Code Online (Sandbox Code Playgroud)
我找到的最好的选项和不太干扰的是在你的xml中设置一个标签参数,比如
电话XML布局
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="phone"/>
Run Code Online (Sandbox Code Playgroud)
平板电脑XML布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="tablet">
...
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
然后在您的活动类中调用它:
View viewPager = findViewById(R.id.pager);
Log.d(getClass().getSimpleName(), String.valueOf(viewPager.getTag()));
Run Code Online (Sandbox Code Playgroud)
希望它对你有用.
归档时间: |
|
查看次数: |
160773 次 |
最近记录: |