React native 获取应用程序运行时的用户位置以及应用程序是否在后台

React native 获取应用程序运行时的用户位置以及应用程序是否在后台,react-native,expo,React Native,Expo,我是React Native的初学者,我正在使用expo我想请求用户访问位置的权限如果用户授予了权限,它将检查手机位置设置是否开启,如果开启,它将给出当前位置,如果应用程序关闭或在后台运行,它仍将给出位置 我试过了,但没用 import React, { Component } from "react"; import { Platform, Text, View, StyleSheet, Linking, Button, AppState } from "reac

我是React Native的初学者,我正在使用expo我想请求用户访问位置的权限如果用户授予了权限,它将检查手机位置设置是否开启,如果开启,它将给出当前位置,如果应用程序关闭或在后台运行,它仍将给出位置

我试过了,但没用


import React, { Component } from "react";
import {
  Platform,
  Text,
  View,
  StyleSheet,
  Linking,
  Button,
  AppState
} from "react-native";
import Constants from "expo-constants";
import * as TaskManager from "expo-task-manager";
import * as Location from "expo-location";
import * as Permissions from "expo-permissions";
import * as IntentLauncher from "expo-intent-launcher";
import { Alert, NavigationActions } from "react-native";
import Modal from "react-native-modal";

const LOCATION_TASK_NAME = "background-location-task";

export default class LocationScreen extends Component {
  state = {
    location: null,
    errorMessage: null,
  };


  componentDidMount = async () => {
    await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
      accuracy: Location.Accuracy.Balanced
    });




  render() {
    let text = "Waiting..";
    if (this.state.errorMessage) {
      text = this.state.errorMessage;
    } else if (this.state.location) {
      text = JSON.stringify(this.state.location);
    }

    return (
      <View style={styles.container}>
        <Text style={styles.paragraph}>{text}</Text>
      </View>
    );
  }
}

TaskManager.defineTask(LOCATION_TASK_NAME, handel);

export async function handel({ data, error }) {
  try {
    let { status } = await Permissions.askAsync(Permissions.LOCATION);

    if (status !== "granted") {
      this.setState({
        errorMessage: "Permission to access location was denied"
      });
      return;
    } else {
      if (data) {
        const { locations } = data;
        const location = locations.map(location => {
          let newLocation = Location.reverseGeocodeAsync(location.coords).then(
            data => {
              let maplocation = data.map(location => {
                console.log(location);
                console.log(location.city);
              });
            }
          );
        });
      }
    }
  } catch (error) {
    let status = Location.getProviderStatusAsync();
    if (!status.locationServiceEnabled) {
      const { navigation } = this.props;
      navigation.navigate("Login");
    }
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: "center",
    justifyContent: "center",
    paddingTop: Constants.statusBarHeight,
    backgroundColor: "#ecf0f1"
  },
  paragraph: {
    margin: 24,
    fontSize: 18,
    textAlign: "center"
  }
});




从“React”导入React,{Component};
进口{
平台,
文本,
看法
样式表,
链接,
按钮
AppState
}从“反应本族语”;
从“expo常量”导入常量;
从“expo任务管理器”导入*作为任务管理器;
从“世博会地点”导入*作为地点;
从“expo Permissions”导入*作为权限;
从“世博会意向启动器”导入*作为意向启动器;
从“react native”导入{Alert,NavigationActions};
从“反应本机模态”导入模态;
const LOCATION_TASK_NAME=“后台位置任务”;
导出默认类位置屏幕扩展组件{
状态={
位置:空,
errorMessage:null,
};
componentDidMount=async()=>{
等待位置。startLocationUpdatesAsync(位置\任务\名称{
准确度:位置、准确度、平衡度
});
render(){
让text=“等待…”;
if(this.state.errorMessage){
text=this.state.errorMessage;
}else if(this.state.location){
text=JSON.stringify(this.state.location);
}
返回(
{text}
);
}
}
TaskManager.defineTask(位置、任务、名称、韩德尔);
导出异步函数handel({data,error}){
试一试{
让{status}=wait Permissions.askAsync(Permissions.LOCATION);
如果(状态!=“已授予”){
这是我的国家({
errorMessage:“访问位置的权限被拒绝”
});
返回;
}否则{
如果(数据){
const{locations}=数据;
const location=locations.map(位置=>{
让newLocation=Location.reverseGeocodeAsync(Location.coords)。然后(
数据=>{
让maplocation=data.map(位置=>{
控制台日志(位置);
console.log(location.city);
});
}
);
});
}
}
}捕获(错误){
让status=Location.getProviderStatusAsync();
如果(!status.locationServiceEnabled){
const{navigation}=this.props;
导航。导航(“登录”);
}
}
}
const styles=StyleSheet.create({
容器:{
弹性:1,
对齐项目:“中心”,
辩护内容:“中心”,
paddingTop:Constants.statusBarHeight,
背景颜色:“ecf0f1”
},
第段:{
差额:24,
尺码:18,
textAlign:“居中”
}
});
这仅在“位置”处于打开状态时有效。如果“位置”处于关闭状态或未授予权限,则不会请求权限