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

18,689

Solution 1

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

Solution 2

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.

Share:
18,689

Related videos on Youtube

naye mtz
Author by

naye mtz

Updated on June 04, 2022

Comments

  • naye mtz
    naye mtz almost 2 years

    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://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/services/foreground-services i have to create a dependencyservice but i don't know how.

  • M Yil
    M Yil about 4 years
    But that won't work right? Because you need a foreground service in order to get the location frequently..
  • M Yil
    M Yil about 4 years
    "MainActivity.Instance". I don't have have a instance, it's null. How can I fix this? : )
  • nevermore
    nevermore about 4 years
    Create a static Instance property in your MainActivity.
  • Daniel Dolz
    Daniel Dolz over 3 years
    Hello. I can't get to find out this "instance". What is it?
  • nevermore
    nevermore over 3 years
    It's a reference of MainActivity.
  • Pavel Polushin
    Pavel Polushin over 3 years
    Hello. Could you please help me with defining the IBinder?