Android Question Service Vs. Receiver Best Practices?

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
I'm in the process of converting an old app I wrote (around B4A v10 or so) whose sole purpose is to query a back-end API every 15 minutes and if any issues are detected/reported with the API it uses NB6 to raise an alert. Currently it has 1 alert icon to indicate it's running (waiting for the next StartServceAt(...) and a separate NB6 icon for "Huston, we have problems."

This app worked fine for years until the latest Android OS update (14). Now, the OS will randomly kill the app even with power-saving off so I decided I'd try re-writing in B4XPages/Receivers even though it's strictly Android.

The app is for personal use and is NOT part of Google (Play). The way the app is currently structured, the "monitoring" is in a service, and it relies on many public variables in the Starter service. The monitor service has a few public subs that activities can call, and if it detects it was started with android.intent.extra.ALARM_COUNT it will show the NB6 notification if a problem is detected, otherwise not (the Activity will handle message notification).

I've read so many posts about problems with services, receivers, what's initialized, what's not initialized, if you can start another service or not, etc., etc., it's hard to tell what path to take. Even Erel's Post on it was somewhat nebulous about design methodology other than "service = continuous process, receiver = short process." The post doesn't explain the receiver life-cycle or how it should interact with the rest of the app.

Given this methodology and everything (wrong, LOL) with Android 14, can someone please suggest which course of action would be best in my use-case? Should I keep it a service and try to find some permission set that will allow it to run as it did before, or is there some design methodology where I can make it a Receiver and not have to duplicate the Starter service code to load credentials, etc.?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You have three options:
1. Use a foreground service. Make sure to start the service when the app is visible. And hope that the OS will not kill the process. The exact behavior depends on the specific device. Whitelisting the app in the settings might help. The "background location tracking" example is based on such service.

2. Use a receiver that is scheduled to start every X minutes (15 minutes period is too short). Every run it should schedules the next run with StartReceiverAt. The receiver will not start the starter service. I'm skeptical whether it will work for many days as a single time where the process fails to start will cause this solution to break.

3. Make a B4J process on your PC that checks for the relevant condition and sends a push notification. In most cases this is the best option.
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
You have three options:
1. Use a foreground service. Make sure to start the service when the app is visible. And hope that the OS will not kill the process. The exact behavior depends on the specific device. Whitelisting the app in the settings might help. The "background location tracking" example is based on such service.

2. Use a receiver that is scheduled to start every X minutes (15 minutes period is too short). Every run it should schedules the next run with StartReceiverAt. The receiver will not start the starter service. I'm skeptical whether it will work for many days as a single time where the process fails to start will cause this solution to break.

3. Make a B4J process on your PC that checks for the relevant condition and sends a push notification. In most cases this is the best option.
Thank you, @Erel, that is what I was afraid of. Option one was what I had working fine for years, then my Phone updated to Android 14 and the app randomly disappeared after some interval.

I will see if I can find some combination of permissions and manifest settings that will work with the B4XPages/B4A and play nice with 14, if I do I'll update this post.
 
Upvote 0
Top