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!

Sign In or Register to comment.