Tablet or Phone - Android

后端 未结 30 2141
难免孤独
难免孤独 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:19

    If screen size detection doesn't return correct value on newer devices, give a try:

    /*
     Returns '1' if device is a tablet
     or '0' if device is not a tablet.
     Returns '-1' if an error occured.
     May require READ_EXTERNAL_STORAGE
     permission.
     */
    public static int isTablet()
    {
        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 ? 1 : 0;
        }
        catch (Throwable t)
        {t.printStackTrace(); return -1;}
    }
    

    Tested on Android 4.2.2 (sorry for my English.)

提交回复
热议问题