如何在 Xamarin.Forms 中创建永无止境的后台服务?

2024-02-04

我每 15 分钟监控一次用户的位置,我只希望应用程序继续发送位置,即使用户关闭任务栏中的应用程序也是如此。

我尝试了这个示例,但它位于 Xamarin.Android 中https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/services/foreground-services https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/services/foreground-services我必须创建一个依赖服务,但我不知道如何创建。


我必须创建一个依赖服务,但我不知道如何创建。

首先,创建一个Interface在 Xamarin.forms 项目中:

public interface IStartService
{

    void StartForegroundServiceCompat();
}

然后创建一个新文件我们称之为itstartServiceAndroid在xxx.Android项目中实现你想要的服务:

[assembly: Dependency(typeof(startServiceAndroid))]
namespace DependencyServiceDemos.Droid
{
    public class startServiceAndroid : IStartService
    {
        public void StartForegroundServiceCompat()
        {
            var intent = new Intent(MainActivity.Instance, typeof(myLocationService));


            if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
            {
                MainActivity.Instance.StartForegroundService(intent);
            }
            else
            {
                MainActivity.Instance.StartService(intent);
            }

        }
    }

    [Service]
    public class myLocationService : Service
    {
        public override IBinder OnBind(Intent intent)
        {
        }

        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            // Code not directly related to publishing the notification has been omitted for clarity.
            // Normally, this method would hold the code to be run when the service is started.

            //Write want you want to do here

        }
    }
}

一旦您想致电StartForegroundServiceCompat中的方法Xamarin.forms项目,您可以使用:

public MainPage()
{
    InitializeComponent();

    //call method to start service, you can put this line everywhere you want to get start
    DependencyService.Get<IStartService>().StartForegroundServiceCompat();

}

这是有关的文档依赖服务 https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/

对于iOS,如果用户关闭任务栏中的应用程序,则将无法再运行任何服务。如果应用程序正在运行,您可以阅读此文档ios 背景演练/位置演练 https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-walkthroughs/location-walkthrough

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Xamarin.Forms 中创建永无止境的后台服务? 的相关文章

  • 如何在 xamarin.ios 中全局隐藏导航栏后退按钮标题

    我在 AppDelegate 中使用了这段代码 UIBarButtonItem Appearance SetBackButtonTitlePositionAdjustment new UIOffset 100 60 UIBarMetrics
  • 无法再在模拟器或设备上调试

    直到今天早上 我才能在物理设备和模拟器上调试我的 Xamarin iOS 应用程序 我认为这与最近的iOS更新有关 当我尝试部署到我的物理设备 iPad 时 我现在收到以下消息 错误 HE0003 无法加载框架 IBFoundation 路
  • sqlite.net + monotouch = SIGSEGV 崩溃

    我们正在使用以下内容 Xamarin 3 Xamarin 表单 单点触控 sqlite net iOS模拟器 硬件 该应用程序在后台线程上与服务器同步数据 整个应用程序只共享一个 SQLite 连接对象 前台查询在后台同步运行的同时执行 所
  • 如何隐藏或删除由xaml创建的特定tableSection(Xamarin.forms)?

    我正在使用 Xamarin form 制作应用程序 我创建了包含 xaml 三个部分的 tableview 我想隐藏或删除最后一部分 整个部分 带有sectionTitle 但不幸的是 Xamarin xaml 不支持条件处理 仅当元素具有
  • XAMARIN - 添加来自 youtube 的视频

    我搜索如何从 youtube 添加视频的信息 例如 我想从一些 YouTube 链接添加视频 我认为它应该在网络视图中 但我需要一些详细信息 因为我找不到有关我的问题的任何信息 您可以使用 webview 播放 youtube 视频 str
  • 启动注册期间无法加载程序集“System.Buffers”

    我的 Xamarin 表单应用程序大约 5 分钟前运行 然后突然停止工作 应用程序启动后立即关闭 下面显示了 Visual Studio 输出中的消息显示 Assembly Loader probing location System Bu
  • Xamarin Forms 自定义地图图钉

    在我正在开发的应用程序之一中 我需要使用自定义地图图钉 并且我已遵循 Xamarin 上的指南https developer xamarin com guides xamarin forms application fundamentals
  • Web 视图中的 Cookie xamarin iOS

    我想在网络视图中设置cookie 对此有什么帮助吗 NSUrl urlq new NSUrl url webview new UIWebView webview LoadRequest new NSUrlRequest urlq webvi
  • GetAsync 请求返回空内容

    我想知道为什么当我在邮递员中尝试它有数据返回时它返回 null 这是我的代码 public async Task
  • Xamarin 构建错误:错误 APT0000:在 ...中,无法找到属性

    我收到了另一位开发商的项目 我设置了我的机器 带有所需组件的 VS 2017 已配置 Android SDK Manager已安装相应版本 项目配置为编译Android 7 1 Nougat 开始构建时出现一个错误 1 gt ERROR e
  • xaml.cs 文件上的 InitializeComponent() 出现错误

    有时我会收到一个红色错误 内容如下 InitializeComponent 在当前上下文中不存在 以及我的其他变量 xaml受约束的x Name The x ClassXaml 文件中的名称空间和类名确实对应于我的xaml cs file
  • 在 MonoDroid 中设置 textview 的文本时,“jobject”不能为 IntPtr.Zero

    我将 MvvmCross 与 MonoDroid 一起使用 在视图模型中的计时器中 我每分钟调用RaisePropertyChanged MinutesRemaining MinutesRemaining是一个整数 指定当前条目结束之前的持
  • Xamarin.iOS 目标 iOS 6 SDK

    我们已升级到 XCode 5 和 Xamarin iOS 7 x 但有一个应用程序尚未准备好支持 iOS 7 有没有办法以 iOS 6 0 SDK 为目标 并编译应用程序 使其具有旧的 iOS 6 键盘等 我们尝试更改 iOS Build
  • Xamarin.forms 长时间运行的后台工作

    我是 Xamarin 平台的新手 我正在使用 Xamarin forms 为所有三个主要平台 WP iOS Android 开发应用程序 如何在后台与我的服务器同步数据 gt 我需要类似于 Android Service 的东西 即使用户转
  • 如何在 Xamarin Forms 中获取视频的长度

    如何使用 Xamarin Forms 获取视频的长度 我已使用以下链接从视频中获取缩略图 但我需要能够获取视频长度 https forums xamarin com discussion 119450 create thumbnail fr
  • 点击当前选项卡刷新页面时的 Xamarin.Forms TabbedPage 事件

    我正在使用 Xamarin Forms 构建 iOS Android 应用程序 并有一个 TabbedPage 如果用户已经在选项卡 2 上 并且单击了选项卡 2 并且我希望刷新选项卡 2 或者运行我自己的函数 以便我可以自己刷新它 有没有
  • 如何在ListView的TextCell中换行文本?

    如何在ListView的TextCell中换行文本 我尝试设置HasUnevenRows to True但这没有帮助 您不能使用 Xamarin 的 开箱即用 TextCell 功能 但是 您应该能够创建一个 ViewCell 并利用换行模
  • 如何将 Hex 或 RGB 颜色分配给 Android Converter 中的 MvxColor

    我有如下转换器 我想返回我自己的十六进制或 RGB 颜色 In iOS public class PinkSelectedWhiteUnselectedValueConverter MvxValueConverter
  • xamarin 表单中标签的数据绑定部分

    我在 Xamarin 表单中使用 Label 我必须显示一个文本 它基本上是一个句子 但该字符串的一部分包含我从 api 调用获得的数字 而字符串的其余部分是固定的 我想使用数据绑定来设置该部分 例子 文字可以是这样的 您肯定可以赢得 0
  • SQLite-Net 扩展 - GetAllWithChildrenAsync 未提取所有内容

    我正在尝试使用 SQLite Net 扩展来创建关系数据库 我在尝试从数据库中提取 Term 对象时遇到了问题 它成功地撤回了其关联的课程 但未撤回与课程关联的评估和笔记 我不确定问题是否在于如何将对象插入数据库 如何从数据库中提取对象 或

随机推荐