walterf25

Expert
Licensed User
Longtime User
Hi all, I'm working on a small app that connects to a power usb meter via BLE, I need to log voltage and current values vs Time to a csv file, my question is, since I haven't used Services in a long time, I'm not sure of which one to use, I now see that there are Receivers, as if our lives are not complex enough 😁.

I need the app to be able to log data while the app is in the background if the user places the app in the background, I know this should be fairly simple, but do you guys have any advise on whether to use a Receiver or a Service to accomplish this task?

What do you recommend and why?

Thanks everyone!
Walter
 

drgottjr

Expert
Licensed User
Longtime User
in general, to keep an app running (almost) all the time, you need a foreground service.
android can still kill your app, regardless. so you need something to bring it back
to life. that would be a receiver. if you wanted your app to be launched as soon as the
device is booted, you would need a receiver. if your app polls the meter periodically for
a reading, a foreground service might be enough. if your app is being used to detect voltage
fluctuations at, eg, the nuclear power station at diablo canyon, good luck and godspeed.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
in general, to keep an app running (almost) all the time, you need a foreground service.
android can still kill your app, regardless. so you need something to bring it back
to life. that would be a receiver. if you wanted your app to be launched as soon as the
device is booted, you would need a receiver. if your app polls the meter periodically for
a reading, a foreground service might be enough. if your app is being used to detect voltage
fluctuations at, eg, the nuclear power station at diablo canyon, good luck and godspeed.
Hi @drgottjr thanks for your reply, I have a service now which seems to be working, however I haven't really done extensive testing as far as how long the service stays running, but eventually I would like the service to run for let's say longer than 1 hour, in this case, you mentioned a Receiver, how would the receiver come into play, how would the receiver re-start the service if it gets killed by the OS?

Regards,
Walter
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
you mentioned a Receiver, how would the receiver come into play, how would the receiver re-start the service if it gets killed by the OS?
I don't like new things, I'm too old, probably. On the other hand, receivers have existed since the first version of Android (API 1), but in B4A we didn't need them.

We should read Erel's tutorial about it (even me, who only read it very quickly).
As far as I understand and remember, receivers are very short-lived and should only be used to launch "something" initially.

For your purpose, you should search on the site how to run "stuff" in the background. You will find that the most often suggested example is "Background location tracking".
Also see this:
https://www.b4x.com/android/forum/threads/play-music-in-the-background.160497/
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
for what it may be worth:
from https://developer.android.com/develop/background-work/background-tasks/broadcasts
"If you declare a broadcast receiver in your manifest, the system launches your app (if the app is not already running) when the broadcast is sent."
this is an exception to the rule. (the rule being you can't start an activity from the background). an alarm broadcast can launch an app when the alarm is triggered.

judging by the service's only being required for an hour, i would think it would continue running if the user simply started another app (as opposed to, eg, swiping to force killing it). i listen to the radio (background) in an android device for around an hour while flipping back and forth between other activities. i suggest rebuilding the app (as is) under b4a 13, running it and coming back after an hour to see what has happened. i would also test by running the app and then launch some other apps to see what has happened to the monitoring service. and go from there. based on what little we know about the app, there may be nothing that currently causes it to fail under android's ever-changing rules. i imagine a user starting the app to set the service in motion and then starting some other app, maybe to check email or to listen to the radio, while the monitoring service runs its course. i would expect that to occur without incident. i don't image the user starting the service and then force killing the app. if that happened, they restart the app and not kill it this time. i mean, really, how much are you supposed to do? if monitoring is supposed to go on all day, then you run the risk of its being killed due to battery preservation issues. more confusing than receiver vs service is the matter of battery conservation.

before looking into added functionality, i would like to hear whether the apps runs as is under android 14 and address that first. if an app has a service that's supposed to run for 1 hour fails to do that, in general, there isn't any particular signal given to that effect. (after the tree falls in the woods, somebody has to come by later to see if anything has happened.) how you deal with that requires knowing more about what's expected of the app. must the service be started at some specific time or all is lost? can it be run at some later time in the event something prevented it from running at the assigned time? perhaps a foreground service is needed, although there is no guarantee that the os does not kill the app. an alarm could trigger a receiver to make sure the service was running (not an alarm app; this is an alarm broadcast receiver). an alarm broadcast might even allow the user to forget about starting the app. if the app runs fine now, then adding a receiver shouldn't be too difficult. if the app doesn't run correctly now, then there's no sense adding a receiver that continues to make it run incorrectly. i use receivers (eg, for sms management or telling me when a usb device has been plugged in), and i've tested with a receiver launching an app that was not running at all. i'd like to know what i don't know before trying to suggest a solution (to a problem that i'm unaware of).
 
Upvote 0

lip

Active Member
Licensed User
Longtime User
I have used Foreground Services to keep things going in the background and recently had to move to Android 14 (API 34). I'm starting the services from the App so don't need any clever way to start them in the background. The solution for me was simply to declare their usage in the manifest, then they seem to work as before. I think this is mainly to help the Playstore review process check that you're not trying to do anything dodgy.

Manifest - change ServiceName to whatever you service is called:
AddPermission(android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE)
SetServiceAttribute(ServiceName, android:foregroundServiceType, "connectedDevice")
 
Upvote 0
Top