Android 判断当前设备是手机还是平板

Android开发需要适配手机和平板,有些需求实现时就要求判断设备是手机还是平板。
网上很多说通过设备尺寸、DPI、版本号、是否具备电话功能等进行判断,不过都不算太精确。
这里分享一个简洁给力的方法(官方用法):

/**
 * 判断当前设备是手机还是平板,代码来自 Google I/O App for Android
 * @param context
 * @return 平板返回 True,手机返回 False
 */
public static boolean isPad(Context context) {
    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK) 
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

令附上是否具备电话功能判断方法(现在部分平板也可以打电话):

public static boolean isPad(Activity activity) {
    TelephonyManager telephony = (TelephonyManager)activity.getSystemService(Context.TELEPHONY_SERVICE);
    if (telephony.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE) {
        return true;
    }else {
        return false;
    }
}

 

至于设备尺寸、分辨率、版本号,以目前手机来看,个人认为精度太差了,不建议使用。

posted @ 2015-01-09 18:46  Lone_Wolf  阅读(10778)  评论(0编辑  收藏  举报