Detecting whether an Android device is a tablet or a phone

You’d like your apps to be compatible with as many devices as possible, but at the same time you want to offer as many features as you can on any device. A common problem for Android developers is determining whether you’re running on a tablet or a phone and a common answer is: Define tablet and phone!

The thing is that you shouldn’t care whether you’re running on a tablet or a phone. The real question you need to ask is whether the current device has the ability to support my feature.

The question of whether a device is a tablet or a phone often boils down to whether or not it supports telephony. Let’s say you have a viral app and you want to enable the users to send out SMS messages to their friends promoting your app. To do this, you don’t really need to know if you’re running on a tablet. All you need to know is whether the device is capable of sending SMS.

here’s a small piece of code that lets you do just that:

/**
* Determines if the current device has telephony capabilities
*
* @param context
* Context through which to access the telephony service
* @return true if the device has a phone radio, false otherwise
*/
public static boolean hasTelephony(Context context) {
    TelephonyManager telMgr = (TelephonyManager) context
        .getSystemService(Context.TELEPHONY_SERVICE);

    if (null == telMgr)
        return false; // no telephony

    return telMgr.getPhoneType() !=
        TelephonyManager.PHONE_TYPE_NONE;
}

Comments, corrections, and improvements are welcome :)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>