-3

I'm trying to differentiate between Android smartphones, tablets and TV box. Is there any way to check that on which device type app is running ?

1 Answers1

0

Yes, you can use following method to detect type of device :

  1. Add a bool in dimension's files to detect Tablet :

Add this line in sw720dp/dimens.xml & sw600dp/dimens.xml

<bool name="isTablet">true</bool>

And in values/dimens.xml. make it false :

<bool name="isTablet">false</bool>
  1. Add this method, and use booleans to detect type :

    boolean isTablet;
    boolean isAndroidTv;
    
    isTablet = (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
    if (!isTablet) {
        isTablet = context.getResources().getBoolean(R.bool.isTablet);
    }
    
    UiModeManager uiModeManager = (UiModeManager) (context.getSystemService(UI_MODE_SERVICE));
    if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
        isAndroidTv = true;
    
    } else if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEVISION) || context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK)) {
        isAndroidTv = true;
    } else if (!context.getPackageManager().hasSystemFeature("android.hardware.touchscreen")) {
        isAndroidTv = true;
    } else if (!context.getPackageManager().hasSystemFeature("android.hardware.telephony")) {
        isAndroidTv = true;
    }
    
NehaK
  • 2,639
  • 1
  • 15
  • 31