ターゲット デバイスが電話かタブレットかを (Android プラットフォームで) プログラムで判断したいと思います。これを行う方法はありますか?Density Metrics を使用して解像度を決定し、それに応じてリソース (画像とレイアウト) を使用しようとしましたが、うまくいきませんでした。電話 (Droid X) とタブレット (Samsung Galaxy 10.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
ジェームズがすでに述べたように、画面サイズをプログラムで決定し、しきい値 Number を使用してロジックを区別できます。
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;
}