React-Native 获取设备信息, Android获取IE

2017-04-05  本文已影响0人  有情怀的程序猿
简单介绍

获取设备信息, 使用的是 react-native-device-info
获取IMEI码参考了react-native-imei源码

关于引入上面的依赖, 链接中都有说明,
算了还是说一遍吧:

**注意: ** 这里只说 Android 的配置步骤, IOS请去链接中查看

1: 获取设备信息 react-native-device-info (不包括IMEI)
npm install --save react-native-device-info 

1: 从新 react-native run-android 你的项目, 也许会有点慢,
如果报错为: can not deleted ............., 请重新运行几次 react-native run-android

var DeviceInfo = require('react-native-device-info');
</Text>: {DeviceInfo.getUniqueID()} </Text>
</Text> :{DeviceInfo.getInstanceID()}</Text>

其他方法见 react-native-device-info

获取后的设备信息
2: react-native-imei 获取手机IMEI码
import { NativeModules, Text, View,} from 'react-native';

export default class TestDeviceInfo extends Component {
  constructor(props) {
    super(props);
    this.state = {
      IMEI: '未获取',
    }
  }

  // 由于是异步获取 , 这里使用 async  ... await更改为同步, 不然获取始终为underfind
  async getIMEI() {
    let IMEI =  await NativeModules.IMEI.getIMEI()
    this.setState({
      IMEI: IMEI,
    })
  }

  componentDidMount() {
    this.getIMEI()
  }
  render() {
      return (
        <View>
              <Text style={styles.instructions}>
                <Text style={{color: '#0366d6'}}> 'IMEI(IMEI码)'</Text>: {this.state.IMEI}
              </Text>
        </View>
      );
    }
}
上一篇 下一篇

猜你喜欢

热点阅读