更新时间:2023-01-25 08:38:25
我最近在我的项目中实现了这样的功能.我确实使用了 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);