【问题标题】:Why doesn't the cordova-plugin-background-mode plugin work on android?为什么 cordova-plugin-background-mode 插件不能在 android 上运行?
【发布时间】:2022-06-11 03:28:32
【问题描述】:

我试图在后台以 30 秒的间隔通过 ajax 发送地理位置,因为此应用程序一直用于了解交付者的“实时”位置。问题是,当您在生成apk并将其安装在手机上时启用android版本中的插件时,它不起作用,即使您将应用程序置于后台并将其放回前台,它也会完全重新启动。 这些是我用于开发应用程序的版本:

  • 科尔多瓦 10
  • Nodejs 14.16
  • jQuery 3.5

结构:

  • js
    • login.js
    • home.js
  • index.html -- 这是login.js文件所在的登录页面
  • home.html -- 这是 home.js 文件所在的主页

login.js

document.addEventListener('deviceready', function () {
  cordova.plugins.backgroundMode.enable();
});

home.js

let isSending = false;
let intervalId = null;
let email = window.localStorage.getItem("user_email");
let token = window.localStorage.getItem("token");
let path = window.localStorage.getItem("api_url");

let onMapSuccess = function (position) {
    let latitude = position.coords.latitude;
    let longitude = position.coords.longitude;
    if (!isSending) {
        $.ajax({
            type: "GET",
            url: path + "/geoTransportista/" + email + "/" + latitude + "/" + longitude,
            headers: {
                Authorization: "Bearer " + token,
                "Content-type": "application/json",
            },
            beforeSend: function() {
                isSending = true;
            }
        }).done((res) => {
            if (res.state == "successful") console.log("ENVIO EXITOSO");
        }).fail((err) => {
            console.log(err);
        }).always(() => {
            isSending = false;
        });
    }
};

let onMapError = function (error) {
    isSending = false;
}

let getLocation = () => {
    navigator.geolocation.getCurrentPosition(onMapSuccess, onMapError, {
        enableHighAccuracy: true,
    });
    return getLocation;
}
document.addEventListener('deviceready', function (e) {
  intervalId = setInterval(getLocation(), 30000);
  cordova.plugins.backgroundMode.on('activate', function (e) {
        cordova.plugins.backgroundMode.disableWebViewOptimizations();
        if (intervalId) clearInterval(intervalId);
        intervalId = setInterval(getLocation(), 30000); 
  });
  cordova.plugins.backgroundMode.on('deactivate', function (e) {
        if (intervalId) clearInterval(intervalId);
        intervalId = setInterval(getLocation(), 30000); 
  });
});

【问题讨论】:

    标签: android cordova cordova-plugins


    【解决方案1】:

    cordova-plugin-background-mode 在 Cordova 10 上不起作用。您可以尝试降级到 Cordova 9。repo 似乎有一段时间不活动了,不确定作者是否会修复它。

    【讨论】:

      猜你喜欢
      • 2020-06-23
      • 2016-05-24
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多