import React from 'react'; import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, Image, TouchableOpacity, View, PermissionsAndroid, DeviceEventEmitter } from 'react-native'; import { init, Geolocation, addLocationListener, start, stop, setInterval, setSensorEnable, setNeedAddress } from "react-native-amap-geolocation"; import GPS from '../../utils/GPS'; import { BjTypeLocation } from '../../utils/types'; import RNBugly from 'rn-bugly'; interface Props { } interface State { } export default class InitLocationTask extends React.PureComponent { constructor(props: Props) { super(props); }; public delayTimer = null; public readonly state: Readonly = { }; async componentDidMount() { this.delayTimer = setTimeout(() => { this.startTask(); }, 2 * 1000); } componentWillUnmount(): void { stop(); this.delayTimer && clearTimeout(this.delayTimer); } async startTask() { await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION ) // await PermissionsAndroid.requestMultiple([ // PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, // PermissionsAndroid.PERMISSIONS // PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION, // PermissionsAndroid.PERMISSIONS.RECORD_AUDIO, // PermissionsAndroid.PERMISSIONS.CAMERA, // PermissionsAndroid.PERMISSIONS.WRITE_CONTACTS, // PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, // PermissionsAndroid.PERMISSIONS.ACCESS_BACKGROUND_LOCATION, // // PermissionsAndroid.PERMISSIONS.WAKE_LOCK // ]); // 使用自己申请的高德 App Key 进行初始化 await init({ ios: "b19f60b1824b2f1ffd9f57275c08bcaa", android: "b19f60b1824b2f1ffd9f57275c08bcaa" }); addLocationListener(location => { this.onLocation(location) }); setNeedAddress(true) setSensorEnable(true) setInterval(1000) // 开始连续定位 start(); this.checkForUpDate(); } onLocation(location: BjTypeLocation) { let newLocation = null; if (location && location.latitude) { newLocation = GPS.gcj_decrypt_exact(Number(location.latitude), Number(location.longitude)) } if (newLocation && newLocation['lat']) { location._latitude = newLocation['lat']; location._longitude = newLocation['lon']; } // console.log("onLocation---->", location) if (location && location.latitude) { DeviceEventEmitter.emit("current-location-update", { location }) } }; checkForUpDate = async () => { console.log("开始更新") // this.loadingToastRef.current.show(); //点击按钮触发检查更新(一般在[关于]-[检查更新]) //此时早已初始化了,不需要延时 RNBugly.checkUpgrade({ //该值为true的时候,会有toast提示,如果不需要提示,设置为false isManual: false, isSilence: false }); // this.loadingToastRef.current.hide(); //获取更新信息 //注意:更新信息获取一次后会储存在本地,即使后台控制停止该版本更新,该方法依旧可以获取到数据 let updateInfo = await RNBugly.getUpgradeInfo(); // console.log("这是updateInfo", updateInfo) } render() { return ( <> ) } }