【问题标题】:How to get Android Version number in React-Native? (not API level number)如何在 React-Native 中获取 Android 版本号? (不是 API 级别编号)
【发布时间】:2019-09-07 07:56:22
【问题描述】:

我想在我的应用中显示 Android 版本(如 8.0、9.0...)。现在我正在使用:Platform.Version 但这会返回 API 版本(24、25...)。

我想要一个比创建映射更优雅的选项,并且不需要每次有新的 android 版本时都重新发布。 有没有办法在没有外部库的情况下做到这一点?

【问题讨论】:

    标签: javascript reactjs react-native native


    【解决方案1】:

    你可以使用这个库,

    react-native-device-info

    import DeviceInfo from "react-native-device-info";
    DeviceInfo.getSystemVersion()
    

    除了版本之外,它还有很多方法可以方便地获取设备特定信息。

    编辑:是getSystemVersion()

    【讨论】:

    • getVerison() 不打印 android 版本本身。它打印应用程序版本。
    【解决方案2】:

    你有两个选择

    1. 使用带有适当版本号的 Platform.Version 映射您获得的值。
    2. 使用 3rd 方库,例如 react-native-device-infogetSystemVersion

      DeviceInfo.getSystemVersion()
      

    【讨论】:

      【解决方案3】:

      你好检查这个组件

      react-native-device-info

      check

      **Gets the API level.**
      
      **Examples**
      
      
      getAPILevel()
      
      const apiLevel = DeviceInfo.getAPILevel();
      
      // iOS: ?
      // Android: 25
      // Windows: ?
      

      【讨论】:

        【解决方案4】:

        您可以使用Platform.constants.Release

        【讨论】:

        • 它有错误:TypeError: undefined is not an object (evaluating '_reactNative.Platform.Constants.Release')
        • @PurTahan 你用的是哪个原生版本?
        • 此常量仅在 Android 上可用,查看文档:reactnative.dev/docs/platform#constants
        【解决方案5】:

        你可以:

        import { Platform } from 'react-native';
        
        const OsVer = Platform.constants['Release'];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-19
          • 1970-01-01
          • 1970-01-01
          • 2018-12-30
          • 2019-02-09
          • 1970-01-01
          相关资源
          最近更新 更多