Forum Xamarin Xamarin.Android

How do I write a background service that will work if the app is cancelled?

First off, thank you ahead of time for any and all help.
I've been at this for a few weeks now, searching for an answer.

I'm writing an Android program (C#) that has a UI, which takes data from a user and passes it's data to a background thread/service to be sent to a server.
The datafirst gets saved to a local database for safe keeping. (Cleanup will be later)

I need a service or something that will guarantee delivery of the data, even if the application is minimized or shut down (swiping it off the screen).
Am I looking for something that does not exist?

I have tried an IntentService, which seemed to do what I wanted but stopped if I killed the UI.
I tried a real Service, which also stopped if I killed the UI, but I may not have set it up properly.

Also, a service seems to run on the UI thread. What good is that? I can't get it to run as a BackgroundWorker either using StartService inside the worker, so I tried running the service as normal and in the StartCommandResult OnStartCommand, calling the data fetch/save method as a Task or Thread. It works as long as the app stays active.
I looked at AsyncTask too, as well as AlarmManager.

What am I missing???
I searched the Xamarin forums as well as the StackOverflow, and found a lot of dead ends.
I'm an old web developer and new to Xamarin by 6 months. I'm not used to the OS deleting my service if I create it. :)

I can post my code if you all need it but I'm truly lost... Someone has done this before.


Sign In or Register to comment.