我正在开发一个通用的android应用程序,我需要检查应用程序是否在平板电脑或手机上运行。有什么方法可以做到吗?
您可以查看Android的Google I/O应用程序src代码:
在UIUtils类中,你有以下方法:public static boolean isHoneycomb() {
// Can use static final constants like HONEYCOMB, declared in later versions
// of the OS since they are inlined at compile time. This is guaranteed behavior.
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
}
public static boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
public static boolean isHoneycombTablet(Context context) {
return isHoneycomb() && isTablet(context);
}
在sdk中,您可以创建具有所需属性的设备(屏幕分辨率,ram,触摸屏,gps ....)
这其实是个有趣的问题。我没有一个绝对的答案,但你可能会在这里找到一些很好的输入如何检测系统信息,如操作系统或设备类型
此外,TelephonyManager. getphonetype()和TelephonyManager中的其他方法可能会感兴趣。
使用Context.getSystemService(Context.TELEPHONY_SERVICE)获取一个TelephonyManager实例
Google Jelly Bean的新实现:
// SystemUI (status bar) layout policy
int shortSizeDp = shortSize
* DisplayMetrics.DENSITY_DEFAULT
/ DisplayMetrics.DENSITY_DEVICE;
if (shortSizeDp < 600) {
// 0-599dp: "phone" UI with a separate status & navigation bar
mHasSystemNavBar = false;
mNavigationBarCanMove = true;
} else if (shortSizeDp < 720) {
// 600-719dp: "phone" UI with modifications for larger screens
mHasSystemNavBar = false;
mNavigationBarCanMove = false;
} else {
// 720dp: "tablet" UI with a single combined status & navigation bar
mHasSystemNavBar = true;
mNavigationBarCanMove = false;
}
}