我正在 Xamarin 表单(PCL - ios 和 android)中执行此应用程序,我正在从服务器获取大量数据(使用 httpclient),我正在显示进度对话框以向用户显示正在获取数据。
假设,用户不小心锁定了 iphone,这些是我看到的在我的应用程序中发生的事情。
- 转到后台。但随后该应用似乎被终止了。
- 解锁手机,应用显示与解锁前相同的屏幕,但没有发生后台进程(可能是因为应用终止(但是,为什么应用终止?))
我的查询:
1. APP为什么不继续取数据?似乎在android中工作正常。
2. iphone 锁定后,任务实际上会发生什么?
3.如果我在pcl中执行http调用,如何在iOS后台继续任务?
Best Answer-推荐答案 strong>
这就是 iOS 和 Android 的不同之处。虽然 Xamarin 使两者的开发变得更容易,但最终它们仍然只是 Android 和 iOS 应用程序,必须遵守该操作系统的规则。
在 iOS 中,从第 9 版开始,您无法在后台运行代码,这与您可以在 Android 中运行的代码相反。对于 iOS,您将不得不为此做一些魔术。
有this blogpost你可以按照它来描述你想要做什么。
他说;
Backgrounding is the term we use for the process of allowing some of
the code in our app to continue to execute while another app is in the
foreground. On iOS, prior to iOS 9, only a single app is allowed to
execute code at a time. This is referred to as the foreground app. If
you don’t change your code to tell iOS that you plan on running code
in the background, your app will be forcefully terminated and removed
from memory if your code attempts to execute in the background.
Android actually does allow code to run in a background activity, but
background activities are one of the first things to be terminated if
the operating system needs more memory. Instead, on Android, we should
use another special class called a Service.
所以还要再看看你是如何在 Android 中做到这一点的,因为如果你做错了,你的传输也会被中断。
为了在 iOS 中实现,他使用这个:
In the AppDelegate.cs file in the iOS project, we will use Messaging
Center to subscribe to the Start and Stop messages. For convenience,
I've wrapped the iOS apis in another class named
iOSLongRunningTaskExample . The important methods here for iOS are
UIApplication.SharedApplication.BeginBackgroundTask ("LongRunningTask", OnExpiration) and
UIApplication.SharedApplication.EndBackgroundTask (taskId) . These are
the methods that tell iOS that we'll be running code in the background
and to not terminate our app.
然后他像这样实现它们:
[Register ("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
MessagingCenter.Subscribe<StartLongRunningTaskMessage> (this, "StartLongRunningTaskMessage", async message => {
longRunningTaskExample = new iOSLongRunningTaskExample ();
await longRunningTaskExample.Start ();
});
MessagingCenter.Subscribe<StopLongRunningTaskMessage> (this, "StopLongRunningTaskMessage", message => {
longRunningTaskExample.Stop ();
});
}
}
public class iOSLongRunningTaskExample
{
nint _taskId;
CancellationTokenSource _cts;
public async Task Start ()
{
_cts = new CancellationTokenSource ();
_taskId = UIApplication.SharedApplication.BeginBackgroundTask ("LongRunningTask", OnExpiration);
try {
//INVOKE THE SHARED CODE
var counter = new TaskCounter();
await counter.RunCounter(_cts.Token);
} catch (OperationCanceledException) {
} finally {
if (_cts.IsCancellationRequested) {
var message = new CancelledMessage();
Device.BeginInvokeOnMainThread (
() => MessagingCenter.Send(message, "CancelledMessage")
);
}
}
UIApplication.SharedApplication.EndBackgroundTask (_taskId);
}
public void Stop ()
{
_cts.Cancel ();
}
void OnExpiration ()
{
_cts.Cancel ();
}
}
详情请看帖子。
关于c# - 如果手机被锁定,则在后台继续 iOS 中的任务,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/39591743/
|