Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Indefinitely running background service X.F Android

z07713z07713 USMember ✭✭

Hey guys! I would like to create an Android background service that will start when my application starts and stays running indefinitely! I have tried serveral ways to achieve this with no luck whatsoever. Yes, i'm using the latest of every possible thing and yes i know it can be achieved only as platform specific, that is fine with me!

Why? It doesn't really matter, but because i want to connect to a netcore signalr server and recieve messages even when the app is not running, and yes i know that i need a broadcast reciever to start it again after the phone restarts i got everything working, except the service stops when the app gets closed.

Thank you!

Best Answer

Answers

  • ivanxamarinivanxamarin Member ✭✭

    What kind of messages do you use SignalR for? Is it possible to switch to data notifications? You can register service to listen for new data notifications and than act on notification (you don't have to present the notification to user). And the service works when the app is in foreground, background or closed (removed from recent).

  • z07713z07713 USMember ✭✭

    @ivanxamarin said:
    What kind of messages do you use SignalR for? Is it possible to switch to data notifications? You can register service to listen for new data notifications and than act on notification (you don't have to present the notification to user). And the service works when the app is in foreground, background or closed (removed from recent).

    it would be various, some messages would be displayed as local notifications, some would trigger data download, etc...

    as I have said, I got it all covered, only I want the service keep running

  • z07713z07713 USMember ✭✭

    @ivanxamarin said:
    @z07713

    Do you override void OnTaskRemoved(Intent rootIntent) in your Service? That's where you should restart the service.

    Anyway, although this probably can be done, I would again suggest data notifications since that's what they are for. You receive them and than you react by displaying notification, download data or update something in local db. This way you are creating service running always to use SignalR to achieve the same thing. Also, I don't know much about OS, but I could never be sure that some custom Android OS have some battery saver which can prevent you from doing this.

    well I'll try to restart it tomorrow, but why does is has to stop at the first place?

    I could switch to data notifications if I has deeper knowledge of it, but I cant imagine how would it integrate with our corporate signalr service

  • ivanxamarinivanxamarin Member ✭✭

    @z07713

    Well, I guess the OS kills all services after the user kills the app (removes from recent) or after some time when it needs resources. Use some debug log to check this.

    Some google search returns this:
    https://stackoverflow.com/questions/24077901/how-to-create-an-always-running-background-service
    Obviously you could have overridden onStart and return START_STICKY, but the KitKat stopped supporting this. That's the reason why you should be careful with this always running services, it can stop working like that after some new android version and some devices with custom OS also might not be supporting it.

  • z07713z07713 USMember ✭✭

    @ivanxamarin said:
    @z07713

    Do you override void OnTaskRemoved(Intent rootIntent) in your Service? That's where you should restart the service.

    Anyway, although this probably can be done, I would again suggest data notifications since that's what they are for. You receive them and than you react by displaying notification, download data or update something in local db. This way you are creating service running always to use SignalR to achieve the same thing. Also, I don't know much about OS, but I could never be sure that some custom Android OS have some battery saver which can prevent you from doing this.

    My friend, that iverride did the trick, exactly what i wanted! Thank you very much!

  • Hey! Stumbled upon this thread and noticed that the task is identical to mine, to get signalR to work regardless of the application state.

    Hope you don't mind if I ask for some advice.

    I have a service in Xamarin.Forms project that provides communication via signalR with the corporate server. I have utilized HostBuilder that is called from the Xamarin.Android project and it has the following service registrations,

    services.AddTransient<IHubEmsConnectionService, HubEmsConnectionService>();
    services.AddSingleton();

    Inside App.xaml.cs I grab an instance of the service HubEmsConnectionService,

    _hubConnectionService = ServiceProvider.GetService();

    This works very nice while application is in the foreground.

    Now to the background work of things, there are so many articles, variations and etc, from third party and official sources, that I cannot seem to wrap my brain around it. If I sit long enough, thinking, reading and trying, I will eventually probably get it, but since you've accomplished the exact same thing, I hope you will be able to provide some simple guidance for me on this matter, or anyone with experience on this.

    What I read is, if I register a class implementation of the BackgroundService in Xamarin.Forms project as a HostedService, it will be registered as a background service on the Android/iOS platforms. I was very sceptic about this, but tried, and obviously without much success. Registration of service is done as such,

    services.AddHostedService();

    public class Worker : BackgroundService

    Worker class is not even initiated.

    Looks like I will have to register a background running service on each platform specifically, Android is a priority. For example, an official sample of registering a service: on GitHub /xamarin/monodroid-samples/blob/master/ApplicationFundamentals/ServiceSamples/StartedServicesDemo/TimestampService.cs (I cannot post full links). It not a problem creating this service, registering in MainActivity, but the main question is, how do I initiate my HubEmsConnectionService in Xamarin.Forms project? Still via Worker?

    Really looking for some guidance on this, thank you in advance!

Sign In or Register to comment.