有没有办法精确检测设备类型(手机、平板电脑、手表、电视、汽车、电脑)?
现在,我找到了一种方法来检测应用程序是否在汽车(uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_CAR
)、电视(uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION
)或手表(uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_WATCH
)上运行。这是正确的吗?连接到汽车上的手机是显示为手机还是显示为"Android Auto"?
为了区分手机、平板电脑或电脑,我可以检查最小屏幕尺寸(例如,600dp才符合平板电脑或笔记本电脑的标准)。
现在的问题是区分平板电脑和笔记本电脑。你有什么想法吗?
附言:我并不是要做一个响应式的用户界面,这是一个与账号的设备管理相关的问题
发布于 2016-10-24 21:13:49
您可以检测是否使用此代码应用程序在大屏幕上运行。
public static boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
这个link也会对你有帮助。
获取屏幕宽度,并使用此断点进行检查。
/*平板电脑(纵向和横向)- */
min-device-width : 768px
max-device-width : 1024px
/*台式机和笔记本电脑- */
min-width : 1224px
发布于 2016-10-24 21:51:01
为了区分手机和平板电脑或电脑,我可以检查最小屏幕尺寸(例如,600dp才符合talet或笔记本电脑的标准)。
有一个更好的方法可以做到这一点,那就是使用值。例如,如果您有两种类型的设备(例如手机和平板电脑),也可以为值创建两个文件夹。然后,对于values文件夹,添加以下内容:
<resources>
<bool name="isLarge">false</bool>
</resources>
在您的values-large文件夹中:
<resources>
<bool name="isLarge">true</bool>
</resources>
然后在你的活动中:
boolean isLarge = getResources().getBoolean(R.bool.isLarge);
if (isLarge) {
// do something
} else {
// do something else
}
使用它,你可以对手机,sw-600dp,sw-720dp等做同样的事情。我不确定你是否可以将它用于电视和其他,但我认为值得一试。
发布于 2016-10-27 20:21:15
请参考此链接,
http://developer.android.com/training/multiscreen/screensizes.html#TaskUseSWQuali
下面我放了检查平板电脑或android电视的代码,请检查一下,它会工作的
用于平板电脑的
private boolean checkIsTablet() {
boolean isTablet;
Display display = ((Activity) this.mContext).getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
float widthInches = metrics.widthPixels / metrics.xdpi;
float heightInches = metrics.heightPixels / metrics.ydpi;
double diagonalInches = Math.sqrt(Math.pow(widthInches, 2) + Math.pow(heightInches, 2));
if (diagonalInches >= 7.0) {
isTablet = true;
}
return isTablet;
}
或
public static boolean checkIsTablet(Context ctx){
return (ctx.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
用于电视的
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private boolean checkIsTelevision() {
boolean isAndroidTV;
int uiMode = mContext.getResources().getConfiguration().uiMode;
if ((uiMode & Configuration.UI_MODE_TYPE_MASK) == Configuration.UI_MODE_TYPE_TELEVISION) {
isAndroidTV = true;
}
它会起作用的,享受吧。
https://stackoverflow.com/questions/40157799
复制相似问题