괴도군의 블로그

[Android] 폰/태블릿 구분코드 (Phone/Tablet device code) 본문

#프로그래밍/Android

[Android] 폰/태블릿 구분코드 (Phone/Tablet device code)

괴도군 2016. 4. 8. 17:28
반응형

첫번째는 화면사이즈..

두번째는 웹뷰를 통한 UserAgent확인!

세번째는 디바이스 정보를 가져와서 확인.. phone / tablet / defalut? 도 에뮬레이터에서는 나오더라구요 


코드를 통해 확인하세요


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
public static boolean isTabletDevice(Context context) {
         
         if (Build.VERSION.SDK_INT >= 19){
         return checkTabletDeviceWithScreenSize(context) &&
                    checkTabletDeviceWithProperties() &&
                 checkTabletDeviceWithUserAgent(context);
         }else{
             return checkTabletDeviceWithScreenSize(context) && 
                    checkTabletDeviceWithProperties() ;
 
         }
     }
     
     private static boolean checkTabletDeviceWithScreenSize(Context context) {
         boolean device_large = ((context.getResources().getConfiguration().screenLayout &
                    Configuration.SCREENLAYOUT_SIZE_MASK) >=
                    Configuration.SCREENLAYOUT_SIZE_LARGE);
 
            if (device_large) {
                DisplayMetrics metrics = new DisplayMetrics();
                Activity activity = (Activity) context;
                activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
 
                if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT
                        || metrics.densityDpi == DisplayMetrics.DENSITY_HIGH
                        || metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM
                        || metrics.densityDpi == DisplayMetrics.DENSITY_TV
                        || metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {
                    return true;
                }
            }
            return false;
     }
     
     private static boolean checkTabletDeviceWithProperties()
     {
         try
         {
             InputStream ism = Runtime.getRuntime().exec("getprop ro.build.characteristics").getInputStream();
             byte[] bts = new byte[1024];
             ism.read(bts);
             ism.close();
 
             boolean isTablet = new String(bts).toLowerCase().contains("tablet");
             return isTablet;
         }
         catch (Throwable t)
         {
             t.printStackTrace(); 
             return false;
         }
     }
     
     private static boolean checkTabletDeviceWithUserAgent(Context context) {
         WebView webView = new WebView(context);
         String ua=webView.getSettings().getUserAgentString();
         webView = null;
         if(ua.contains("Mobile Safari")){
             return false;
         }else{
            return true;
         }
     }
 
 
 
 
cs


반응형
Comments