如何知道平板电脑或手机是否其在机器人编程? [英] How to know whether its a tablet or phone in android programmatically?
本文介绍了如何知道平板电脑或手机是否其在机器人编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获得检测指定设备是否处于android.I平板电脑或手机都试过两人在模拟器但没有奏效。两者都是在这里:
首先
IF((getResources()getConfiguration()屏幕布置及放大器; Configuration.SCREENLAYOUT_SIZE_MASK)== Configuration.SCREENLAYOUT_SIZE_LARGE)
{
// code
}
二
私人布尔isTabletDevice()
{
如果(android.os.Build.VERSION.SDK_INT> = 11)
{
// 蜂窝
//测试屏幕大小,使用反射因为isLayoutSizeAtLeast仅自11
配置CON = getResources()getConfiguration()。
尝试 {
方法mIsLayoutSizeAtLeast = con.getClass()实现getMethod(isLayoutSizeAtLeast)。
布尔R =(布尔)mIsLayoutSizeAtLeast.invoke(CON,0x00000004); // Configuration.SCREENLAYOUT_SIZE_XLARGE
返回ř;
}赶上(例外X)
{
返回false;
}
}
返回false;
}
解决方案
试试这个code。你可以在屏幕英寸
字符串inputSystem;
inputSystem = android.os.Build.ID;
Log.d(海,inputSystem);
显示显示= getWindowManager()getDefaultDisplay()。
INT宽度= display.getWidth(); //德precated
INT高= display.getHeight(); //德precated
Log.d(海,宽+);
Log.d(海,高度+);
DisplayMetrics DM =新DisplayMetrics();
。getWindowManager()getDefaultDisplay()getMetrics(DM)。
双X = Math.pow(宽度/ dm.xdpi,2);
双Y = Math.pow(身高/ dm.ydpi,2);
双screenInches =的Math.sqrt(X + Y);
Log.d(海,屏幕英寸:+ screenInches +);
I would like to get to detect whether the given device is a tablet or phone in android.I have tried the two in the simulator but none worked. Both are here:
First
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE)
{
//code
}
Second
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");
Boolean r = (Boolean) mIsLayoutSizeAtLeast.invoke(con, 0x00000004); // Configuration.SCREENLAYOUT_SIZE_XLARGE
return r;
} catch (Exception x)
{
return false;
}
}
return false;
}
解决方案
Try this code. You can get the screen inches
String inputSystem;
inputSystem = android.os.Build.ID;
Log.d("hai",inputSystem);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
Log.d("hai",width+"");
Log.d("hai",height+"");
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double x = Math.pow(width/dm.xdpi,2);
double y = Math.pow(height/dm.ydpi,2);
double screenInches = Math.sqrt(x+y);
Log.d("hai","Screen inches : " + screenInches+"");
这篇关于如何知道平板电脑或手机是否其在机器人编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文