Tablet or Phone - Android

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

    Well, the best solution that worked for me is quite simple:

    private boolean isTabletDevice(Resources resources) {   
        int screenLayout = resources.getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
        boolean isScreenLarge = (screenLayout == Configuration.SCREENLAYOUT_SIZE_LARGE);
        boolean isScreenXlarge = (screenLayout == Configuration.SCREENLAYOUT_SIZE_XLARGE);
        return (isScreenLarge || isScreenXlarge);
    }
    

    Used like this:

    public void onCreate(Bundle savedInstanceState) {
        [...]
        if (this.isTabletDevice(this.getResources()) == true) {
            [...]
        }
    }
    

    I really don't want to look at the pixels sizes but only rely on the screen size.

    Works well as Nexus 7 (LARGE) is detected as a tablet, but not Galaxy S3 (NORMAL).

提交回复
热议问题