2011-08-31 62 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; 

} 
相關問題