【问题标题】:Detect whether the device is phone, tablet or tv on flutter在flutter上检测设备是手机、平板还是电视
【发布时间】:2020-12-21 00:17:26
【问题描述】:

我想知道使用的设备是否是电视。我正在使用flutter_device_type包,但它只检测平板电脑并将任何其他设备视为手机

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    默认情况下,flutter 根据设备的最小尺寸(大于最小宽度的屏幕被视为平板电脑)检测设备,类似于以下代码:

      String getDeviceType() {
        final data = MediaQueryData.fromWindow(WidgetsBinding.instance.window);
        return data.size.shortestSide < 600 ? 'phone' :'tablet';
      }
    

    另一方面,使用 Java/Kotlin 并基于 this documentation,您可以使用以下代码检测设备是否为 android Tv:

    val uiModeManager = getSystemService(UI_MODE_SERVICE) as UiModeManager
    if (uiModeManager.currentModeType == Configuration.UI_MODE_TYPE_TELEVISION) {
        Log.d(TAG, "Running on a TV Device")
    } else {
        Log.d(TAG, "Running on a non-TV Device")
    }
    

    因此,正确的选择是使用本机代码并使用 platform-channels 将其连接到颤振

    【讨论】:

      【解决方案2】:

      您可以使用device_info_plus 包来检测Android TV。

      AndroidDeviceInfo 类包含 list of all the system's features,如 Android PackageManager 中所述

      为了检测电视,您可以使用例如:

      DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      bool isTV = androidInfo.systemFeatures.contains('android.software.leanback_only')
      

      【讨论】:

      • 它有效,非常感谢这个答案。如果设备是平板电脑或智能电视,来自 MediaQuery 的 IMO ShortestSide 将给出相同的结果。
      猜你喜欢
      • 2012-07-05
      • 2015-05-28
      • 2018-09-04
      • 1970-01-01
      • 2012-05-19
      • 1970-01-01
      • 2016-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多