Android Question Controlling a service

FFMan

Member
Licensed User
Longtime User
I'm working on a small test program to try and understand how the service apps work.

I want to run a service that I use intensely for say 2 x 30 mins every 3 weeks or so. at the moment I run it as a foreground app but several things can push it to background and stop it working properly so service seems the way to go.

I understand that Android wants to handle resources and I don't mind my background service running when not needed so long as it doesn't consume cpu time, but at the moment I don't understand how I can control it other than have it poll for a global command variable from the associated foreground app. clearly polling this variable will keep the service pretty active when 99% of the time I don't need it.

Is there a way of forcing an event in a background service, so the app can be idle until the event wakes it ?

Also, how are the service & foreground app linked, it seems if I kill one, the other dies too. I though I could kill the foreground app and the service would continue.
 

DonManfred

Expert
Licensed User
Longtime User
What exactly you want to poll from your main app? Did you need it in real time (polling every x ms) or what else?
If we know what the goal is the help could be easier.

Actually i would say

On servicestart poll your information and then stop service and shedule a new start in x seconds, minutes, hours, whatever...
See ServiceStartAt
 
Upvote 0

FFMan

Member
Licensed User
Longtime User
the service app (when required to) is getting data every 5 seconds from the internet, transforming it and sending it on via Bluetooth. the foreground app is of no consequence and could (if possible) not even be running when the service is active. the only reason I need the foreground app I can see is to stop and start the service.

when I do not need the data I want the service to sleep or die
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Then you should not shedule a next service-start after stopping the service (this can be done from the service too if i remember correctly; you dont need the app running for that...

In service you poll the data from internet and after recieving the result you stop the service and shedule an next run 5 secongs in future. As long as it need you can retrieve data..
 
Upvote 0
Top