首页 > 解决方案 > 与 iOS 相比,Android 上的地理围栏速度较慢

问题描述

我正在开发一个使用 Location SDK ( https://docs.expo.io/versions/latest/sdk/location/ ) 对一个区域进行地理围栏的 Expo 项目。

地理围栏在 iOS 上运行良好,但是,在 Android 上,它在后台(或当应用程序被强制退出时)可能真的很慢。退出或进入地理围栏时,运行注册任务有时可能需要 15 分钟以上。

我还注意到,如果我打开谷歌地图等应用程序并点击 GPS 按钮,我可以强制触发已注册的地理围栏任务运行。

是否有可能以某种方式加速位置更新,或者在 Android Studio 中配置一些东西?

package.json 包括:

需要帮助请叫我。

import React, { useEffect, useState } from "react";
import { StyleSheet, Text, View } from "react-native";
import {
  getForegroundPermissionsAsync,
  requestBackgroundPermissionsAsync,
  requestForegroundPermissionsAsync,
  startGeofencingAsync,
} from "expo-location";
import * as Notifications from "expo-notifications";
import { LocationGeofencingEventType } from "expo-location";
import * as TaskManager from "expo-task-manager";

Notifications.setNotificationHandler({
  handleNotification: async () => ({
    shouldShowAlert: true,
    shouldPlaySound: false,
    shouldSetBadge: false,
  }),
});

TaskManager.defineTask(
  "GEOFENCE_TASK",
  ({ data: { eventType, region }, error }) => {
    if (error) {
      // check `error.message` for more details.
      return;
    }
    if (eventType === LocationGeofencingEventType.Enter) {
      console.log("You've entered region:", region);
      Notifications.scheduleNotificationAsync({
        content: {
          title: "ENTERED GEOFENCE",
          body: region.identifier,
        },
        trigger: null,
      });
    } else if (eventType === LocationGeofencingEventType.Exit) {
      console.log("You've left region:", region);
      Notifications.scheduleNotificationAsync({
        content: {
          title: "EXITED GEOFENCE",
          body: region.identifier,
        },
        trigger: null,
      });
    }
  }
);

export default function App() {
  const [isLoading, setIsLoading] = useState(true);
  useEffect(() => {
    const setUp = async () => {
      const { granted: notificationsGranted } =
        await Notifications.getPermissionsAsync();
      if (!notificationsGranted) {
        await Notifications.requestPermissionsAsync();
      }
      const { granted: fgGranted } = await getForegroundPermissionsAsync();
      if (!fgGranted) {
        await requestForegroundPermissionsAsync();
        await requestBackgroundPermissionsAsync();
      }

      const geofences = [
        {
          identifier: "Stockholm",
          latitude: 59.332598,
          longitude: 18.035258,
          radius: 100,
          notifyOnEnter: true,
          notifyOnExit: true,
        },
      ];
      await startGeofencingAsync("GEOFENCE_TASK", geofences);
    };

    setUp();
  }, []);

  return (
    <View style={styles.container}>
      {isLoading ? <Text>App is Loading</Text> : <Text>Loading done</Text>}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
  },
});

标签: react-nativeexpo

解决方案


我发现了同样的“问题”并为了缓解它,我使用前台通知来改进地理围栏服务。

好消息是,该技巧适用于“应用程序正在使用”权限。(但后台位置权限是强制性的)

问候。


推荐阅读