Tablet or Phone - Android

后端 未结 30 2712
难免孤独
难免孤独 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:18

    below method is calculating the device screen's diagonal length to decide the device is a phone or tablet. the only concern for this method is what is the threshold value to decide weather the device is tablet or not. in below example i set it as 7 inch and above.

    public static boolean isTablet(Activity act)
    {
        Display display = act.getWindow().getWindowManager().getDefaultDisplay();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        display.getMetrics(displayMetrics);
    
        float width = displayMetrics.widthPixels / displayMetrics.xdpi;
        float height = displayMetrics.heightPixels / displayMetrics.ydpi;
    
        double screenDiagonal = Math.sqrt( width * width + height * height );
        int inch = (int) (screenDiagonal + 0.5);
        Toast.makeText(act, "inch : "+ inch, Toast.LENGTH_LONG).show();
        return (inch >= 7 );
    }
    

提交回复
热议问题