tablet2+android,Tablet or Phone - Android

这篇博客提供了一段Java代码,用于检测Android设备是否为平板。代码通过检查屏幕尺寸(至少7英寸,Xlarge级别)和密度(至少MDPI)来判断。这对于根据设备类型实现不同屏幕布局的逻辑非常有用。
摘要由CSDN通过智能技术生成

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.

/**

* Checks if the device is a tablet or a phone

*

* @param activityContext

* The Activity Context.

* @return Returns true if the device is a Tablet

*/

public static boolean isTabletDevice(Context activityContext) {

// Verifies if the Generalized Size of the device is XLARGE to be

// considered a Tablet

boolean xlarge = ((activityContext.getResources().getConfiguration().screenLayout &

Configuration.SCREENLAYOUT_SIZE_MASK) ==

Configuration.SCREENLAYOUT_SIZE_XLARGE);

// If XLarge, checks if the Generalized Density is at least MDPI

// (160dpi)

if (xlarge) {

DisplayMetrics metrics = new DisplayMetrics();

Activity activity = (Activity) activityContext;

activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

// MDPI=160, DEFAULT=160, DENSITY_HIGH=240, DENSITY_MEDIUM=160,

// DENSITY_TV=213, DENSITY_XHIGH=320

if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT

|| metrics.densityDpi == DisplayMetrics.DENSITY_HIGH

|| metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM

|| metrics.densityDpi == DisplayMetrics.DENSITY_TV

|| metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {

// Yes, this is a tablet!

return true;

}

}

// No, this is not a tablet!

return false;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值