如何在Xamarin.Forms中创建永无止境的后台服务? [英] How to create a never ending background service in Xamarin.Forms?

查看:529
本文介绍了如何在Xamarin.Forms中创建永无止境的后台服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am monitoring the user's location every 15 minutes and I just want the application to continue sending the location even if the user closes the application in the taskbar.

我尝试了此示例,但它在Xamarin.Android中

I tried this sample but it's in Xamarin.Android https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/services/foreground-services i have to create a dependencyservice but i don't know how.

推荐答案

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

i have to create a dependencyservice but i don't know how.

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

First, create an Interface in the Xamarin.forms project:

public interface IStartService
{

    void StartForegroundServiceCompat();
}

然后创建一个新文件,在xxx.Android项目中将其命名为itstartServiceAndroid,以实现所需的服务:

And then create a new file let's call it itstartServiceAndroid in xxx.Android project to implement the service you want:

[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

        }
    }
}

一旦要在Xamarin.forms项目中调用StartForegroundServiceCompat方法,则可以使用:

Once you want to call the StartForegroundServiceCompat method in Xamarin.forms project, you can use:

public MainPage()
{
    InitializeComponent();

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

}

以下是有关依赖关系的文档服务

对于iOS,如果用户在任务栏中关闭应用程序,则您将不再能够运行任何服务.如果应用程序正在运行,则可以阅读有关

For iOS, if the user closes the application in the taskbar, you will no longer be able to run any service. If the app is running, you can read this document about ios-backgrounding-walkthroughs/location-walkthrough

这篇关于如何在Xamarin.Forms中创建永无止境的后台服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆