如何编程方式确定的Android目标设备?
问题描述:
我想以编程方式确定(在Android平台上),如果目标设备是手机或平板电脑。
有没有办法做到这一点?
我试着使用密度指标,以确定相应的分辨率和使用的资源(图片和布局),但它并没有变成好。还有,当我发动对电话(Droid X的)应用程序的差异和平板电脑(三星Galaxy 10.1)。
I would like to programmatically determine (on the Android platform), if the target device is a phone or a tablet. Is there a way to do this? I tried using Density Metrics to determine the resolution and used resources (images and layouts) accordingly but, it did not turn out well. There are differences when I launch the app on a phone (Droid X) and a tablet (Samsung Galaxy 10.1).
请指教。
答
您可以使用此code
You can use this code
private boolean isTabletDevice() {
if (android.os.Build.VERSION.SDK_INT >= 11) { // honeycomb
// test screen size, use reflection because isLayoutSizeAtLeast is only available since 11
Configuration con = getResources().getConfiguration();
try {
Method mIsLayoutSizeAtLeast = con.getClass().getMethod("isLayoutSizeAtLeast", int.class);
Boolean r = (Boolean) mIsLayoutSizeAtLeast.invoke(con, 0x00000004); // Configuration.SCREENLAYOUT_SIZE_XLARGE
return r;
} catch (Exception x) {
x.printStackTrace();
return false;
}
}
return false;
}