Tablet or Phone - Android

后端 未结 30 2131
难免孤独
难免孤独 2020-11-22 08:33

Is there a way to check if the user is using a tablet or a phone? I\'ve got problems with my tilt function and my new tablet (Transformer)

相关标签:
30条回答
  • 2020-11-22 09:10

    Please check out below code.

    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;
    }
    
    0 讨论(0)
  • 2020-11-22 09:12

    why use this?

    Use this method which returns true when the device is a tablet
    
    public boolean isTablet(Context context) {  
    return (context.getResources().getConfiguration().screenLayout   
        & Configuration.SCREENLAYOUT_SIZE_MASK)    
        >= Configuration.SCREENLAYOUT_SIZE_LARGE; 
    }
    

    i see many ways above.the Configuration class has get the right answer just below:

        /**
     * Check if the Configuration's current {@link #screenLayout} is at
     * least the given size.
     *
     * @param size The desired size, either {@link #SCREENLAYOUT_SIZE_SMALL},
     * {@link #SCREENLAYOUT_SIZE_NORMAL}, {@link #SCREENLAYOUT_SIZE_LARGE}, or
     * {@link #SCREENLAYOUT_SIZE_XLARGE}.
     * @return Returns true if the current screen layout size is at least
     * the given size.
     */
    public boolean isLayoutSizeAtLeast(int size) {
        int cur = screenLayout&SCREENLAYOUT_SIZE_MASK;
        if (cur == SCREENLAYOUT_SIZE_UNDEFINED) return false;
        return cur >= size;
    }
    

    just call :

     getResources().getConfiguration().
     isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE);
    

    it's ok?

    0 讨论(0)
  • 2020-11-22 09:13

    Why not calculate the size of the screen diagonal and use that to make the decision whether the device is a phone or tablet?

    private boolean isTablet()
    {
        Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        display.getMetrics(displayMetrics);
    
        int width = displayMetrics.widthPixels / displayMetrics.densityDpi;
        int height = displayMetrics.heightPixels / displayMetrics.densityDpi;
    
        double screenDiagonal = Math.sqrt( width * width + height * height );
        return (screenDiagonal >= 9.0 );
    }
    

    Of course one can argue whether the threshold should be 9 inches or less.

    0 讨论(0)
  • 2020-11-22 09:14

    In the Google IOSched 2017 app source code, the following method is used:

    public static boolean isTablet(Context context) {
        return context.getResources().getConfiguration().smallestScreenWidthDp >= 600;
    }
    
    0 讨论(0)
  • 2020-11-22 09:16

    For me the distinction between phone and tablet is not size of device and/or pixel density, which will change with technology and taste, but rather the screen ratio. If I'm displaying a screen of text I need to know if, say, 15 long lines are needed (phone) vs 20 shorter lines (tablet). For my game I use the following for a ballpark estimation of the rectangle my software will be dealing with:

        Rect surfaceRect = getHolder().getSurfaceFrame();
        screenWidth = surfaceRect.width();
        screenHeight = surfaceRect.height();
        screenWidthF = (float) screenWidth;
        screenHeightF = (float) screenHeight;
        widthheightratio = screenWidthF/screenHeightF;
        if(widthheightratio>=1.5) {
            isTablet=false;
        }else {
            isTablet=true;
        }
    
    0 讨论(0)
  • 2020-11-22 09:17

    To detect whether or not the device is a tablet use the following code:

    public boolean isTablet(Context context) {
        boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE);
        boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
        return (xlarge || large);
    }
    

    LARGE and XLARGE Screen Sizes are determined by the manufacturer based on the distance from the eye they are to be used at (thus the idea of a tablet).

    More info : http://groups.google.com/group/android-developers/browse_thread/thread/d6323d81f226f93f

    0 讨论(0)
提交回复
热议问题