【问题标题】:ionic vue background geolocation tracking stops after 5 minuteionic vue 背景地理定位跟踪在 5 分钟后停止
【发布时间】:2023-07-26 07:57:01
【问题描述】:

我正在使用插件
https://github.com/seididieci/capacitor-backround-geolocation 观察用户的位置。然后我在推杆的帮助下跟踪那个位置。此背景位置仅在停止后工作 5 分钟。我正在使用电容器的后台任务。但该插件还会在用户打开应用程序后将数据保存在手机上。后台任务向推送器发送数据。

这是手表定位功能

  getLocation: async function () {
      BackgroundGeolocation.initialize({
        notificationText: "Your app is running, tap to open.",
        notificationTitle: "App Running",
        updateInterval: 10000,
        requestedAccuracy: BgGeolocationAccuracy.HIGH_ACCURACY,
        // Small icon has to be in 'drawable' resources of your app
        // if you does not provide one (or it is not found) a fallback icon will be used.
        smallIcon: "ic_small_icon",
        // Start getting location updates right away. You can set this to false or not set at all (se below).
        startImmediately: true,
      });
      // const geolocation = new Geolocation.Geolocation();
      BackgroundGeolocation.addListener("onLocation", (location) => {
        // console.log("Got new location", location);
        this.subscribe(location.longitude, location.latitude);
        console.log(location)
      });
    
      BackgroundGeolocation.addListener("onPermissions", (location) => {
        // console.log("BGLocation permissions:", location);
        this.subscribe(location.longitude, location.latitude);
        
        // Do something with data
      });
      BackgroundGeolocation.start();
    },

然后在mounted()中调用函数

mounted(){
this.getLocation
  App.addListener("appStateChange", (state) => {
       setInterval(this.getLocation, 120000);
      if (!state.isActive) {
        BackgroundTask.beforeExit(async () => {
          setInterval(this.getLocation, 120000);
          console.og('Why')
        });
      }
      if (state.isActive) {
        setInterval(this.getLocation, 120000);
        console.log('Active')
      }
    });
}

【问题讨论】:

    标签: vue.js ionic-framework geolocation capacitor ionic5


    【解决方案1】:

    您需要像这样使用https://ionicframework.com/docs/native/foreground-service 来运行后台。

    【讨论】: