反应本机博览会背景位置不起作用

时间:2019-08-25 09:23:51

标签: react-native location expo

我是React Native的新手,我正在使用expo,如果应用程序在后台或被杀死,我试图获取用户位置。我使用了Taskmanager,但我不明白为什么它不起作用。当应用程序在前台运行时,它可以正常工作,但是当应用程序在后台运行或被杀死时,则无法提供我的位置。

我正在使用expo-location中的Location


const LOCATION_TASK_NAME = "background-location-task";

componentDidMount = async () => {

        this._getLocationAsync();
  };

_getLocationAsync = async () => {
    try {
      //ask permission of user for location
      let { status } = await Permissions.askAsync(Permissions.LOCATION);

      //check the location setting
      const permissionStaatus = await Location.getProviderStatusAsync();
      const newStatus = permissionStaatus.locationServicesEnabled;

      //if phone location is disabled
      if (!newStatus) {
        const { navigation } = this.props;
        Alert.alert(
          "Error",
          "Please Turn On you'r Location",
          [
            {
              text: "OK",
              onPress: this.openSetting
            }
          ],
          { cancelable: false }
        );
      }
      // if location setting is enabled
      else {
        //if status is granted or not
        if (status !== "granted") {
          this.setState({
            errorMessage: "Permission to access location was denied"
          });
          return;
        } else {
          await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
            accuracy: Location.Accuracy.Balanced,
            timeInterval: 3000,
            distanceInterval: 1
          });
        }
      }
    } catch (error) {
      let status = await Location.getProviderStatusAsync();
      if (!status.locationServiceEnabled) {
        const { navigation } = this.props;
        navigation.navigate("Site");
        // console.log(error);
      }
    }
  };



TaskManager.defineTask(LOCATION_TASK_NAME, async ({ data, error }) => {
  if (error) {
    // Error occurred - check `error.message` for more details.
    return;
  }
 if (data) {
    const { locations } = data;
  const location = locations.map(location => {
   let newLocation = Location.reverseGeocodeAsync(location.coords)
        .then(data => {
          data.map(location => {
            axios.post(
              "http://192.168.0.1/api/account/saveUserLocation",
              location
            );
          })

        })
    })
}
});

3 个答案:

答案 0 :(得分:0)

当应用程序在应用程序const PostTypeSelection = ({ client }) => { const [search, updateSearch] = useState(""); const [tags, updateTags] = useState([]); const handleChange = e => { const { value } = e.target; client .query({ query: GET_TAGS, variables: { search: search } }) .then(({ data }) => { console.log("I'm hit"); updateTags(data.get_tags); }); updateSearch(value); }; console.log(search); return ( <div> <p>Select tags you'll want to see</p> <input type="text" name="search" placeholder="Type tag name" onChange={handleChange} /> {tags.map(tag => { return ( <div key={tag.id}> {tag.name} ({tag.count}) </div> ); })} </div> ); }; 上时,您将无法运行x = int((shape[21][0]+shape[22][0])/2) y = int((shape[21][1]+shape[22][1])/2) w = abs(shape[39][0]-shape[42][0]) h = abs(y-shape[29][1]) y_mid = int((y + (y - h)) / 2) roi = image[y-h:y_mid,int(x-w/2):int(x+w/2)] if(roi.shape[0] !=0 and roi.shape[1] != 0): roi = imutils.resize(roi, width=250, inter=cv2.INTER_CUBIC) cv2.imshow('ROI', roi) cv2.rectangle(image,(int(x-w/2), int(y-h)), (int(x + w/2), y_mid), (0, 0, 255), 1) cv2.imshow('Image', image) ajaxbackground在存储库中的存储。因此,如果您想在应用程序失效时获得TaskManager,请尝试更新location

App.js

location

更新位置

location

答案 1 :(得分:0)

您需要为android启用即时贴通知,新版android操作系统的行为有所不同。 自Android(8.0)起

您必须使用粘性通知来使您的应用程序保持在前台。 您需要将这些选项传递给listT<-list() number <- 0 for(a in 0:3){ for(i in 1:(5-a)){ for(j in 1:(5-a)){ A<-matrix(0,nrow=5,ncol=5) A[c(i:(i+a)),c(j:(j+a))]<-1 number<-number+1 listT[[number]]<-A } } } vectors<-matrix(0,25,54) for (number in 1:54){ vectors[,number] <- t(as.vector(listT[[number]])) }

startLocationUpdatesAsync

foregroundService: { notificationTitle: 'Your title', notificationBody: 'Notification Body' }, pausesUpdatesAutomatically: false, } 用于IOS,如果您希望您的应用在被杀死时重新启动。

注意:就目前而言,任务管理器和后台位置仅适用于博览会项目, 它不适用于最低限度的博览会项目。

答案 2 :(得分:0)

我在文档中说的foregroundService方法中使用了startLocationUpdatesAsync选项:

使用此选项可将位置服务置于前台状态,这将使后台位置更新的频率与在前台状态时一样频繁。不利的一面是,它需要即时通知,因此用户将意识到您的应用程序正在运行,即使在后台运行也会消耗更多资源。 (自Android 8.0起可用)

这是我代码中的样子:

Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, { 
    accuracy: Location.Accuracy.BestForNavigation,
    timeInterval: LOCATION_TIME_INTERVAL,
    distanceInterval: LOCATION_DISTANCE_INTERVAL,
    foregroundService: {
        notificationTitle: LOCATION_TITLE,
        notificationBody: LOCATION_SUBTITLE
    }
});