简体   繁体   中英

Android 4.0+ tablet keyword?

I heard that there is a keyword or method that Android developers can use to determine whether Android 4.0+ is running on a tablet (without having to do any kludgy screen size comparisons). I can't find it.

So, is this true? If so, what is it?

Edit: I see that people are giving me answers that are not what I'm looking for. I already know how to determine whether a device is a tablet or phone using the kludgy screen size method and using the OS method. I'm looking for a method that has been available starting with API level 14 that will tell me the same information -- hopefully. I can't find it, so I'm wondering whether it even exists.

Tablet basically just means a big(er) screen, so it comes down to that. There's an easy way of seeing if it's a tablet sized screen or not:

if ( getResources().getConfiguration().isLayoutSizeAtLeast( Configuration.SCREENLAYOUT_SIZE_XLARGE ) )
{
    //Is a tablet.
}

You could change Configuration.SCREENLAYOUT_SIZE_XLARGE to Configuration.SCREENLAYOUT_SIZE_LARGE if you want to catch smaller ones too.

As described in the android documentation, the SDK level (integer) the phone is running is available in:

android.os.Build.VERSION.SDK_INT;

The enum corresponding to this int is in the android.os.Build.VERSION_CODES class.

Code example:

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.FROYO){
    // Do something for froyo and above versions
} else{
    // do something for phones running an SDK before froyo
}

Edit: This SDK_INT is available since Donut (android 1.6 / API4) so make sure your application is not retro-compatible with Cupcake (android 1.5 / API3) when you use it or your application will crash (thanks to Programmer Bruce for the precision).

Link

Android 4+ has the configuration object . You can use the "isLayoutSizeAtLeast(int size)" method to do your check. It's less kludgy than some other examples I've seen around.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM