How to create a never ending background service in Xamarin.Forms?
Asked Answered
C

2

8

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.

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

Chelsea answered 25/9, 2019 at 22:53 Comment(2)
For Android I can say it can never be guaranteed a background service will always be running, because of some limitations such as when device's resources is under presser, also Google has imposed further limitations in newer Android versions, or even limitations some manufacturers apply. One possible choice for Android would be using Foreground Services though.Diopside
Thank you it is important for me this informationChelsea
L
6

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

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

public interface IStartService
{

    void StartForegroundServiceCompat();
}

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

        }
    }
}

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();

}

Here is the document about dependency-service

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

Laundry answered 26/9, 2019 at 8:49 Comment(5)
"MainActivity.Instance". I don't have have a instance, it's null. How can I fix this? : )Lastly
Create a static Instance property in your MainActivity.Laundry
Hello. I can't get to find out this "instance". What is it?Aniseikonia
It's a reference of MainActivity.Laundry
Hello. Could you please help me with defining the IBinder?Mateusz
F
6

You might want to have a look at Shiny by Allan Ritchie. It's currently in beta but I would still suggest using it, as it will save you a lot of trouble writing this code yourself. Here's a blog post by Allan, explaining what you can do with Shiny in terms of background tasks - I think Scheduled Jobs are the thing you're looking for.

Franke answered 26/9, 2019 at 2:49 Comment(1)
But that won't work right? Because you need a foreground service in order to get the location frequently..Lastly
L
6

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

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

public interface IStartService
{

    void StartForegroundServiceCompat();
}

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

        }
    }
}

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();

}

Here is the document about dependency-service

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

Laundry answered 26/9, 2019 at 8:49 Comment(5)
"MainActivity.Instance". I don't have have a instance, it's null. How can I fix this? : )Lastly
Create a static Instance property in your MainActivity.Laundry
Hello. I can't get to find out this "instance". What is it?Aniseikonia
It's a reference of MainActivity.Laundry
Hello. Could you please help me with defining the IBinder?Mateusz

© 2022 - 2024 — McMap. All rights reserved.