如果我关闭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
)}
如果应用程序转到前台,它可以正常运行但是一旦我关闭应用程序,我正在运行的任务也会停止。 那么即使应用程序关闭,有没有任何解决方法/方法使我的任务运行?
修改
我还为iOS和Andorid添加了权限,但我得到了相同的结果。
编辑2:
我在后台尝试做的是编写自己的重要位置更改服务实现,因为没有适用于iOS和Android的Cordova或PhoneGap的免费插件。
答案 0 :(得分: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);
这是一个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);
答案 1 :(得分:2)
我认为您尝试实施的背景地理位置跟踪已经作为cordova插件存在,它被称为cordova-plugin-mauron85-background-geolocation。
此插件是前台和后台地理定位服务。它比html5地理定位或cordova-geolocation插件更具电池和数据效率。
有很多配置选项,请参阅上面链接的github页面。
答案 2 :(得分:0)
IONIC-3的解决方法
导入插件
For i = LastRow To 2 Step -1
If Cells(i, "A").Value <> Application.Evaluate("MAX(IF(" & Range(Cells(1, "B"), Cells(LastRow, "B")).Address _
& "=" & Cells(i, "B").Address & "," & Range(Cells(1, "A"), Cells(LastRow, "A")).Address & "))") Then
Rows(i).Delete
End If
Next i
添加构造函数
(BOOL)isUpdateAvailable:(NSString*)latestVersion
{
NSString *currentAppVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
NSArray *myCurrentVersion = [currentAppVersion componentsSeparatedByString:@"."];
NSArray *myLatestVersion = [latestVersion componentsSeparatedByString:@"."];
NSInteger legthOfLatestVersion = myLatestVersion.count;
NSInteger legthOfCurrentVersion = myCurrentVersion.count;
if (legthOfLatestVersion == legthOfCurrentVersion)
{
for (int i=0; i<myLatestVersion.count; i++)
{
if ([myCurrentVersion[i] integerValue] < [myLatestVersion[i] integerValue])
{
return true;
}
else if ([myCurrentVersion[i] integerValue] == [myLatestVersion[i] integerValue])
{
continue;
}
else
{
return false;
}
}
return false;
}
else
{
NSInteger count = legthOfCurrentVersion > legthOfLatestVersion ? legthOfLatestVersion : legthOfCurrentVersion;
for (int i=0; i<count; i++)
{
if ([myCurrentVersion[i] integerValue] < [myLatestVersion[i] integerValue])
{
return true;
}
else if([myCurrentVersion[i] integerValue] > [myLatestVersion[i] integerValue])
{
return false;
}
else if ([myCurrentVersion[i] integerValue] == [myLatestVersion[i] integerValue])
{
continue;
}
}
if (legthOfCurrentVersion < legthOfLatestVersion)
{
for (NSInteger i=legthOfCurrentVersion; i<legthOfLatestVersion; i++)
{
if ([myLatestVersion[i] integerValue] != 0)
{
return YES;
}
}
return NO;
}
else
{
return NO;
}
}
}