.Net MAUI (Android) 如何在后台下载文件

Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,456 信誉分 Microsoft 外部员工
2024-07-05T05:46:58.5033333+00:00

你好,

 

有没有办法在 Android 设备中在后台执行一些操作?例如,在我的应用程序中,我有一个回调,在用户按下下载按钮后开始下载文件。此文件非常大,某些设备可能会在下载完成之前关闭屏幕。这导致下载重置,用户必须再次按下按钮。我的回调函数如下所示:

private void DownloadButton_Click(object sender, EventArgs e)
{
    try
    {
        WebClient webClient = new WebClient();
        webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
        webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadCompletedCallback);
        webClient.DownloadFileAsync(downloadUrl, downloadPath);
    }
    catch (Exception ex)
    {
        ...
    }
}

 

此问题整理于:[https://learn.microsoft.com/en-us/answers/questions/1662507/net-maui-downloading-a-file-in-the-background-(and](https://learn.microsoft.com/en-us/answers/questions/1662507/net-maui-downloading-a-file-in-the-background-(and)

开发人员技术 | .NET | .NET 多平台应用 UI
0 个注释 无注释

1 个答案

排序依据: 非常有帮助
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,166 信誉分 Microsoft 外部员工
    2024-07-05T05:47:47.0033333+00:00

    你好,

    在 Android 中,如果需要在后台执行任务,则需要将功能实现为后台服务。这是 Android 的原生功能,因此您可以参考以下文档并在 MAUI 中实现它。

    This guide discusses Xamarin.Android services, which are Android components that allow work to be done without an active user interface. Services are very commonly used for tasks that are performed in the background, such as time consuming calculations, downloading files, playing music, and so on. It explains the different scenarios that services are suited for and shows how to implement them both for performing long-running background tasks as well as for providing an interface for remote procedure calls.

    尽管本文档适用于 Xamarin,但在使用原生功能方面,MAUI 与 Xamarin 没有什么不同。


    如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。 注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。

    此答案是否有帮助?

    0 个注释 无注释

你的答案

提问者可以将答案标记为“已接受”,审查方可以将答案标记为“已推荐”,这有助于用户了解答案是否解决了提问者的问题。