在后台获取 Ionic/Cordova 应用程序中的位置 [英] Get location in Ionic/Cordova app when in background
问题描述
如果我关闭 Ionic/Cordova 应用程序(适用于 iOS 和 Android),是否可以运行后台服务?
Is it possible to run a background service if I close my Ionic/Cordova app (both for iOS and Android) ?
出于这个目的,我选择了插件 https://github.com/katzer/cordova-plugin-background-模式
For the purpose I picked that pluging https://github.com/katzer/cordova-plugin-background-mode
到目前为止我有那个代码:
So far I have that code:
$ionicPlatform.ready(function () {
cordova.plugins.backgroundMode.isEnabled();
cordova.plugins.backgroundMode.configure({
silent: true
})
............
///do some task
)}
如果应用程序转到前台,它工作正常,但是一旦我关闭应用程序,我正在运行的任务也会停止.那么,即使应用程序关闭,是否有任何解决方法/方法可以使我的任务运行?
It works fine if the app goes to the foreground but as soon as I close the application the task I am running stops as well. So is there any workaround/way to make my task running even if the app is closed ?
我还为 iOS 和 Andorid 添加了权限,但得到了相同的结果.
I have also added permissions for both iOS and Andorid but I am getting the same result.
编辑 2:
我在后台尝试做的是编写我自己的重要位置更改服务的实现,因为没有适用于 iOS 和 Android 的 Cordova 或 PhoneGap 免费插件.
What am I trying to do in background is to write my own implementation of significant location-change service since there is no free plugin for Cordova or PhoneGap that can work with both iOS and Android.
推荐答案
我最近在我的项目中实现了这样的功能.我确实使用了 Ionic,并且确实使用了 Katzer 的 Cordova 插件背景模式.(现在我正在通过 iOS 9.2 模拟器运行后台进程).
I've recently implemented a feature like this in my project. I did use Ionic and I did use the Cordova plugin background mode from Katzer. (right now I'm running the background process through the iOS 9.2 simulator).
这是让它工作的代码片段:
Here's a code snippet to get it working:
// Run when the device is ready
document.addEventListener('deviceready', function () {
// Android customization
// To indicate that the app is executing tasks in background and being paused would disrupt the user.
// The plug-in has to create a notification while in background - like a download progress bar.
cordova.plugins.backgroundMode.setDefaults({
title: 'TheTitleOfYourProcess',
text: 'Executing background tasks.'
});
// Enable background mode
cordova.plugins.backgroundMode.enable();
// Called when background mode has been activated
cordova.plugins.backgroundMode.onactivate = function () {
// Set an interval of 3 seconds (3000 milliseconds)
setInterval(function () {
// The code that you want to run repeatedly
}, 3000);
}
}, false);
这是一个准备好 ES6 的 Ionic 2 示例:
Here's an Ionic 2 example ES6 ready:
// Import the Ionic Native plugin
import { BackgroundMode } from 'ionic-native';
// Run when the device is ready
document.addEventListener('deviceready', () => {
// Android customization
// To indicate that the app is executing tasks in background and being paused would disrupt the user.
// The plug-in has to create a notification while in background - like a download progress bar.
BackgroundMode.setDefaults({
title: 'TheTitleOfYourProcess',
text: 'Executing background tasks.'
});
// Enable background mode
BackgroundMode.enable();
// Called when background mode has been activated
// note: onactive now returns an returns an observable that emits when background mode is activated
BackgroundMode.onactivate.subscribe(() => {
// The code that you want to run repeatedly
});
}, false);
这篇关于在后台获取 Ionic/Cordova 应用程序中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!