Tablet or Phone - Android

后端 未结 30 2499
难免孤独
难免孤独 2020-11-22 08:33

Is there a way to check if the user is using a tablet or a phone? I\'ve got problems with my tilt function and my new tablet (Transformer)

30条回答
  •  春和景丽
    2020-11-22 09:13

    Why not calculate the size of the screen diagonal and use that to make the decision whether the device is a phone or tablet?

    private boolean isTablet()
    {
        Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        display.getMetrics(displayMetrics);
    
        int width = displayMetrics.widthPixels / displayMetrics.densityDpi;
        int height = displayMetrics.heightPixels / displayMetrics.densityDpi;
    
        double screenDiagonal = Math.sqrt( width * width + height * height );
        return (screenDiagonal >= 9.0 );
    }
    

    Of course one can argue whether the threshold should be 9 inches or less.

提交回复
热议问题