IT_Programming/Android_Java

[Tip] 폰과 태블릿 구별하기

JJun ™ 2013. 2. 5. 04:34


  출처

   http://v.daum.net/link/39058461?CT=WIDGET

   http://stackoverflow.com/questions/9478793/how-to-know-its-phone-or-tablet




1. 아래 방법은 기기가 들고있는 DP와 pixel을 곱해 가상의 flaot를 생성해내고 

    태블릿과 폰을 테스트하며 2 이상인 경우 태블릿이라는 판단을 내리는 방법입니다. 

    (한국의 어느 분의 소스라는데, 한번 거쳐서 받아 출처가 확실치는 않네요. 감사하게 사용하고 있습니다.)



// Tablet 인지 유무 파악
    protected boolean isTablet()
    {
        int portrait_width_pixel = Math.min(this.getResources().getDisplayMetrics().widthPixels, this.getResources().getDisplayMetrics().heightPixels);
        int dots_per_virtual_inch = this.getResources().getDisplayMetrics().densityDpi;
        float virutal_width_inch = portrait_width_pixel / dots_per_virtual_inch;
         
        return (virutal_width_inch > 2);
    }






2. found in Chrome to Phone extension example


static boolean isTablet (Context context) {
        // TODO: This hacky stuff goes away when we allow users to target devices
        int xlargeBit = 4; // Configuration.SCREENLAYOUT_SIZE_XLARGE;  // upgrade to HC SDK to get this
        Configuration config = context.getResources().getConfiguration();
        return (config.screenLayout & xlargeBit) == xlargeBit;
    }






3. Based on the GDD 2011 sample application I will be using these helper methods

public static boolean isHoneycomb() {
    // Can use static final constants like HONEYCOMB, declared in later versions
    // of the OS since they are inlined at compile time. This is guaranteed behavior.
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
}
public static boolean isTablet(Context context) {
    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
public static boolean isHoneycombTablet(Context context) {
    return isHoneycomb() && isTablet(context);
}