2011-08-31 119 views
0

我想以编程方式确定(在Android平台上),如果目标设备是手机或平板电脑。 有没有办法做到这一点? 我尝试使用密度度量来确定分辨率并相应地使用资源(图像和布局),但是效果不佳。我在手机(Droid X)和平板电脑(Samsung Galaxy 10.1)上启动应用程序时存在差异。如何在Android中以编程方式确定目标设备?

请指教。

+0

你看到什么类型的差异?看看这一点,并实现您的显示器尺寸和硬件设计:http://groups.google.com/group/android-developers/browse_thread/thread/d6323d81f226f93f –

+0

谢谢你的参考,詹姆斯。 不同之处在于渲染中的工件。与平板电脑相比,在手机上进行测试时,对象不会呈现在相同位置。 – Anon

+0

那么问题现在解决了吗? –

回答

0

正如James已经提到的,您可以通过编程方式确定屏幕大小,并使用阈值Number来区分逻辑之间的区别。

1

您可以使用此代码

private boolean isTabletDevice() { 

if (android.os.Build.VERSION.SDK_INT >= 11) { // honeycomb 
    // test screen size, use reflection because isLayoutSizeAtLeast is only available since 11 
    Configuration con = getResources().getConfiguration(); 
    try { 
     Method mIsLayoutSizeAtLeast = con.getClass().getMethod("isLayoutSizeAtLeast", int.class); 
     Boolean r = (Boolean) mIsLayoutSizeAtLeast.invoke(con, 0x00000004); // Configuration.SCREENLAYOUT_SIZE_XLARGE 
     return r; 
    } catch (Exception x) { 
     x.printStackTrace(); 
     return false; 
    } 
} 
return false; 
} 

链接:http://www.androidsnippets.com/how-to-detect-tablet-device

+0

这是不正确的,因为ICS现在可以在平板电脑和手机上使用,并且它的SDK是> = 11。 – Vlad

+0

Vlad,检查版本是否大于等于11后,检查版面大小是否为XLARGE – Aracem

0

基于Aracem的回答,我更新了正常的平板电脑检查代码段为3.2或更高版本(sw600dp):

public static boolean isTablet(Context context) { 
    try { 
     if (android.os.Build.VERSION.SDK_INT >= 13) { // Honeycomb 3.2 
      Configuration con = context.getResources().getConfiguration(); 
      Field fSmallestScreenWidthDp = con.getClass().getDeclaredField("smallestScreenWidthDp"); 
      return fSmallestScreenWidthDp.getInt(con) >= 600; 
     } else if (android.os.Build.VERSION.SDK_INT >= 11) { // Honeycomb 3.0 
      Configuration con = context.getResources().getConfiguration(); 
      Method mIsLayoutSizeAtLeast = con.getClass().getMethod("isLayoutSizeAtLeast", int.class); 
      Boolean r = (Boolean) mIsLayoutSizeAtLeast.invoke(con, 0x00000004); // Configuration.SCREENLAYOUT_SIZE_XLARGE 
      return r; 
     } 
    } catch (Exception e) { 
    } 
    return false; 

} 
相关问题