菜鸟教程小白 发表于 2022-12-12 12:07:20

c# - 如果手机被锁定,则在后台继续 iOS 中的任务


                                            <p><p>我正在 Xamarin 表单(PCL - ios 和 android)中执行此应用程序,我正在从服务器获取大量数据(使用 httpclient),我正在显示进度对话框以向用户显示正在获取数据。</p >

<p>假设,用户不小心锁定了 iphone,这些是我看到的在我的应用程序中发生的事情。</p>

<ol>
<li>转到后台。但随后该应用似乎被终止了。</li>
<li>解锁手机,应用显示与解锁前相同的屏幕,但没有发生后台进程(可能是因为应用终止(但是,为什么应用终止?))</li>
</ol>

<p>我的查询:
1. APP为什么不继续取数据?似乎在android中工作正常。
2. iphone 锁定后,任务实际上会发生什么?
3.如果我在pcl中执行http调用,如何在iOS后台继续任务?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这就是 iOS 和 Android 的不同之处。虽然 Xamarin 使两者的开发变得更容易,但最终它们仍然只是 Android 和 iOS 应用程序,必须遵守该操作系统的规则。</p>

<p>在 iOS 中,从第 9 版开始,您无法在后台运行代码,这与您可以在 Android 中运行的代码相反。对于 iOS,您将不得不为此做一些魔术。
有<a href="http://arteksoftware.com/backgrounding-with-xamarin-forms/" rel="noreferrer noopener nofollow">this blogpost</a>你可以按照它来描述你想要做什么。</p>

<p>他说;</p>

<blockquote>
<p>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.</p>
</blockquote>

<p>所以还要再看看你是如何在 Android 中做到这一点的,因为如果你做错了,你的传输也会被中断。</p>

<p>为了在 iOS 中实现,他使用这个:</p>

<blockquote>
<p>In the <code>AppDelegate.cs</code> file in the iOS project, we will use Messaging
Center to subscribe to the Start and Stop messages. For convenience,
I&#39;ve wrapped the iOS apis in another class named
<code>iOSLongRunningTaskExample</code>. The important methods here for iOS are
<code>UIApplication.SharedApplication.BeginBackgroundTask (&#34;LongRunningTask&#34;, OnExpiration)</code> and
<code>UIApplication.SharedApplication.EndBackgroundTask (taskId)</code>. These are
the methods that tell iOS that we&#39;ll be running code in the background
and to not terminate our app.</p>
</blockquote>

<p>然后他像这样实现它们:</p>

<pre><code>
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    public override bool FinishedLaunching (UIApplication app, NSDictionary options)
    {
      MessagingCenter.Subscribe&lt;StartLongRunningTaskMessage&gt; (this, &#34;StartLongRunningTaskMessage&#34;, async message =&gt; {
            longRunningTaskExample = new iOSLongRunningTaskExample ();
            await longRunningTaskExample.Start ();
      });

      MessagingCenter.Subscribe&lt;StopLongRunningTaskMessage&gt; (this, &#34;StopLongRunningTaskMessage&#34;, message =&gt; {
            longRunningTaskExample.Stop ();
      });
    }
}

public class iOSLongRunningTaskExample
{
    nint _taskId;
    CancellationTokenSource _cts;

    public async Task Start ()
    {
      _cts = new CancellationTokenSource ();

      _taskId = UIApplication.SharedApplication.BeginBackgroundTask (&#34;LongRunningTask&#34;, 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 (
                  () =&gt; MessagingCenter.Send(message, &#34;CancelledMessage&#34;)
                );
            }
      }

      UIApplication.SharedApplication.EndBackgroundTask (_taskId);
    }

    public void Stop ()
    {
      _cts.Cancel ();
    }

    void OnExpiration ()
    {
      _cts.Cancel ();
    }
}
</code></pre>

<p>详情请看帖子。</p></p>
                                   
                                                <p style="font-size: 20px;">关于c# - 如果手机被锁定,则在后台继续 iOS 中的任务,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39591743/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39591743/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: c# - 如果手机被锁定,则在后台继续 iOS 中的任务