Tablet or Phone - Android

2018-12-31 09:23发布

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)

29条回答
深知你不懂我心
2楼-- · 2018-12-31 10:21

I think this is the easiest way to be honest. This will check the screen size that's being used:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();

Best of luck!

查看更多
只若初见
3楼-- · 2018-12-31 10:21

To detect whether or not the device is a tablet use the following code:

public boolean isTablet(Context context) {
    boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE);
    boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
    return (xlarge || large);
}

LARGE and XLARGE Screen Sizes are determined by the manufacturer based on the distance from the eye they are to be used at (thus the idea of a tablet).

More info : http://groups.google.com/group/android-developers/browse_thread/thread/d6323d81f226f93f

查看更多
唯独是你
4楼-- · 2018-12-31 10:22

This is the method that i use :

public static boolean isTablet(Context ctx){    
    return = (ctx.getResources().getConfiguration().screenLayout 
    & Configuration.SCREENLAYOUT_SIZE_MASK) 
    >= Configuration.SCREENLAYOUT_SIZE_LARGE; 
}

Using:

Configuration.SCREENLAYOUT_SIZE_MASK

Configuration.SCREENLAYOUT_SIZE_LARGE

This is the recommended method!

查看更多
有味是清欢
5楼-- · 2018-12-31 10:22

This method is a recommend by Google. I see this code in Google Offical Android App iosched

public static boolean isTablet(Context context) {
        return (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK)
                >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
查看更多
看风景的人
6楼-- · 2018-12-31 10:24

Use this method which returns true when the device is a tablet

public boolean isTablet(Context context) {  
    return (context.getResources().getConfiguration().screenLayout   
        & Configuration.SCREENLAYOUT_SIZE_MASK)    
        >= Configuration.SCREENLAYOUT_SIZE_LARGE; 
}
查看更多
琉璃瓶的回忆
7楼-- · 2018-12-31 10:24

If you are only targeting API level >= 13 then try

public static boolean isTablet(Context context) {
    return context.getResources().getConfiguration().smallestScreenWidthDp >= 600;
}

cheers :-)

查看更多
登录 后发表回答