rad*_*tiv 33 background background-service cordova ionic-framework
如果我关闭我的Ionic/Cordova应用程序(iOS和Android),是否可以运行后台服务?
为此,我选择了插入https://github.com/katzer/cordova-plugin-background-mode
到目前为止我有这个代码:
$ionicPlatform.ready(function () {
cordova.plugins.backgroundMode.isEnabled();
cordova.plugins.backgroundMode.configure({
silent: true
})
............
///do some task
)}
Run Code Online (Sandbox Code Playgroud)
如果应用程序转到前台,它可以正常工作,但是一旦我关闭应用程序,我运行的任务也会停止.那么即使应用程序关闭,有没有任何解决方法/方法可以让我的任务运行?
编辑:
我还为iOS和Andorid添加了权限,但我得到了相同的结果.
编辑2:
我在后台尝试做的是编写自己的重要位置更改服务实现,因为没有适用于iOS和Android的Cordova或PhoneGap的免费插件.
0x1*_*ad2 13
我最近在我的项目中实现了这样的功能.我确实使用了Ionic,我确实使用了Katzer的Cordova插件背景模式.(现在我正在通过iOS 9.2模拟器运行后台进程).
这是一个让它工作的代码片段:
// 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);
Run Code Online (Sandbox Code Playgroud)
这是一个Ionic 2示例ES6准备就绪:
// 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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25670 次 |
| 最近记录: |