Tablet or Phone - Android

后端 未结 30 2144
难免孤独
难免孤独 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;
    }
    

提交回复
热议问题