安卓识别平板和手机

版权声明:转载请@我原创地址 https://blog.csdn.net/weixin_39706415/article/details/83990162

 通过反复测试比较靠谱的就是google提供的api 使用分辨率识别多少寸的的那个现在压根就不准确

/**
 * 判断当前设备是手机还是平板,代码来自 Google I/O App for Android
 * 常数screenlayout:一个screenlayout_size_mask值指示屏幕至少约为480x640 dp单位,对应于 大资源限定符。
 * 1440*1920
 * 常数screenlayout:编码比特的大小
 *
 * @return 平板返回 True,手机返回 False
 */
private static boolean isPad(Context context) {
    return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

 下面这个不建议使用

/**
     * 判断是否为平板
     */
    private static boolean isPads() {
        WindowManager wm = (WindowManager) MyApplication.getAppContext().getSystemService(Context.WINDOW_SERVICE);
        assert wm != null;
        Display display = wm.getDefaultDisplay();
        // 屏幕宽度
        float screenWidth = display.getWidth();
        // 屏幕高度
        float screenHeight = display.getHeight();
        DisplayMetrics dm = new DisplayMetrics();
        display.getMetrics(dm);
        double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
        double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
        // 屏幕尺寸
        double screenInches = Math.sqrt(x + y);
        // 大于6尺寸则为Pad
        return screenInches >= 6.0;
    }

猜你喜欢

转载自blog.csdn.net/weixin_39706415/article/details/83990162
今日推荐