0

I did one application for both mobile and Tablet. Here I want visible few of my contents on Mobile but not in Tablet.

How can I Detect whether Android device is a phone or a tablet while launching my activity.

I Google and find out few suggestions like User -agent.. How can I use it or any other solutions??

Thanks in advance...

4

4 回答 4

1

好吧,当谈到为不同类型的屏幕尺寸使用不同的资源时,最好的文章是"Supporting Multiple Screens".

我检测设备类型的最简单方法是strings.xml为单个键输入不同的值,我们称之为"device_type". 所以在:

  1. 价值观 ->strings.xml我将拥有<string name="device_type">smartphone</string>
  2. 值-大->strings.xml我将拥有<string name="device_type">tablet</string>
  3. 值-小->strings.xml我会有<string name="device_type">small</string>
  4. values-xlarge ->strings.xml我会有<string name="device_type">xlarge</string>

有了上下文,我将读取此值并确定我拥有的设备类型。

于 2013-07-31T11:25:08.740 回答
1
public static boolean isTablet (Context context) 
{

    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

如果是平板电脑,这将返回 true,如果是移动设备,则返回 false

于 2013-07-31T11:15:38.620 回答
1
TelephonyManager manager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);

if (manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE)
{
     //It's a tablet
}
else
{
     //It's a phone
}

它应该有效

于 2013-07-31T11:17:52.980 回答
0

利用

public static boolean isTablet(Context context) {
    return hasHoneycomb() && isTab(context);
}

具有配套功能

     public static boolean hasHoneycomb() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
    }

    public static boolean isTab(Context context) {
        return (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK)
                >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }
于 2013-07-31T11:26:54.260 回答