Android Question Android ends service!

Helio

Member
I'm just writing to try to understand when a service running in the foreground is disabled by the Android system. I developed an alarm app and after choosing the text and time data for the alarms, I close the app and only its service is running in the foreground, sometimes it works for a day, sometimes two, but suddenly, just like that, it is stopped by Android with no apparent error. After a while it works again, but then it gives an error because the alarm data disappears with the service, as I store it in a list. I even used a code so that the app doesn't optimize the battery.
I saw a discussion more or less on the topic in 2019, but as it was very old I decided to post another one.
I have two questions:
1 - This is normal, as I have an alarm app that always works, even with the battery optimizer being marked.
2 - Will I have to use a database to store the alarm data and when Android turns it on again I load it from it?
 

Helio

Member
I use StartServiceAt, in fact StartServiceAtExact. Is its erratic functioning because I'm using service instead of a receiver? Because there are apps that always work, like alarm apps, like the one I'm trying to make. I would like to know if this problem only occurs with B4A apps or any other, as I have an alarm app, called SmPlan, which I have been using for over a year without any problems, I have even restarted the device and it continued to work with previously added alarms. Does the system not destroy it due to the company's reputation or is there something else?
Another question. If the system destroys and restarts the app at a scheduled time, it means it is placed in a queue, am I right? This is where the database comes in if there is no other way. When it restarts I would put back the alarm data saved in the database.

Anyone who could have an insight into this? I responded to Erel's answer, but it seems to have been deleted!
 
Upvote 0

Helio

Member
Então, por que você usa um serviço em primeiro plano? Eu não entendo o que você está fazendo.
Isn't it the right one for these types of alarm apps for daily tasks?

I'm making an alarm app that notifies me of daily tasks. My Android version is 6.01 and, for now, I'm developing the app just for myself. Hence the questions.
- Does the system place an app in the queue and, after closing it, it restarts with the next task? And if so, as it restarts empty, since I store the data in a list, would it be good to create a database to restore the data?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Does the system place an app in the queue and, after closing it, it restarts with the next task?
StartReceiverAt will cause the process to start. Unless the device was booted or the app was explicitly killed by the user (in the second case the behavior is not consistent).

since I store the data in a list, would it be good to create a database to restore the data?
Best to use KeyValueStore: https://www.b4x.com/android/forum/threads/b4x-kvs-keyvaluestore-library.120234/
 
Upvote 0

Helio

Member
As for your first answer, I don't know if I made myself understood correctly. I asked if when the Android system destroys my app to save memory, that is, kill the foreground service that is running, does it put it in a queue to restart it in the next task?

And the question is that if this happens can I retrieve the task data with a database?

As for your suggestion to use KeValueStore, I will try to use it as it seems more practical. Thanks!
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
does it put it in a queue to restart it in the next task?
No. It will not be restarted.

And the question is that if this happens can I retrieve the task data with a database?

As for your suggestion to use KeValueStore, I will try to use it as it seems more practical. Thanks!
If you use KVS then the data is always available.
 
Upvote 0

Helio

Member
Apparently, there is no way to create an alarm app since the system destroys your service, even if it is in the foreground. I just still don't understand how there are so many apps of this type that work normally.

Anyway, thanks for the response!
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Apparently, there is no way to create an alarm app since the system destroys your service
You seem to think that an alarm will occur only if your app, or a service, is running. That is not true. You ask the AlarmManager to raise an alarm; that will be done even if your app is not running. If you followed the link that Erel has pointed out you would discover this yourself.

Alarms (based on the AlarmManager class) give you a way to perform time-based operations outside the lifetime of your application. For example, you could use an alarm to initiate a long-running operation, such as starting a service once a day to download a weather forecast.

The key words here are " .. . outside the lifetime of your application". I believe (it is a long while since I looked at this closely, so I may be wrong) that the alarm can start a service that could then set a new alarm, so that you can easily create an alarm that will run at the same time every day
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
The key words here are " .. . outside the lifetime of your application". I believe (it is a long while since I looked at this closely, so I may be wrong) that the alarm can start a service that could then set a new alarm, so that you can easily create an alarm that will run at the same time every day
The "proper" way to do is these days is to use a Broadcast Receiver. From that you can schedule the next alarm, post a notification (from which the user can open your app), etc...

- Colin.
 
Upvote 0

Helio

Member
-Where do I put the 'Service_Create' and 'Service_Start' codes for a service in a receiver? Would I put everything in 'Receiver_Receive' and would I put the Service_Create code after 'If FirstTime = True'?

-Will I no longer need to use 'Service.AutomaticForegroundMode' or 'Service.StartForeground(nid, CreateNotification(""))'? Does this mean that the receiver automatically triggers scheduled alarms even without visible activity?
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
-Where do I put the 'Service_Create' and 'Service_Start' codes for a service in a receiver? Would I put everything in 'Receiver_Receive' and would I put the Service_Create code after 'If FirstTime = True'?

-Will I no longer need to use 'Service.AutomaticForegroundMode' or 'Service.StartForeground(nid, CreateNotification(""))'? Does this mean that the receiver automatically triggers scheduled alarms even without visible activity?
https://www.b4x.com/android/forum/threads/receivers-and-services-in-2023.145370/
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
If you read this tutorial from start to finish, you will see that it does not answer any of my questions. Thank you anyway!
You haven't provided any real detail about how your app is intended to work, so how do you expect anyone to answer your questions? Why do you need a service? What are you doing after the alarm is raised? If you're using the alarm to create a notification that will open your app when you tap on it, you don't need a service. If you're also scheduling a new alarm after you've created the notification, you still don't need a service.

I have an app that allows users to create appointments & reminders. It uses AlarmManager to create alarms & a BroadcastReceiver to post a notification when the alarm is raised by the system. It also sets the next alarm in the BroadcastReceiver. When the user taps on the notification, the app opens (or comes into the foreground if it's already running in the background) & displays the details about the appointment/reminder. I don't use a service for any of the functionality - in fact I don't use any services at all in the app (unless you count AlarmManager as a service - which technically it is (it's a system service)). [Disclaimer] The app is now written in Kotlin, but when I originally wrote it in B4A it had the same functionality.

If you search the forum for StartServiceAt & also use the link I posted in my previous reply, you should have everything you need to make your app work.

- Colin.
 
Last edited:
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
If you read this tutorial from start to finish, you will see that it does not answer any of my questions.
I am afraid that that is because you are asking the wrong questions. I have not used Receivers before but I have just done some reading and have written this demo. Once started it issues a Log message every six seconds. Close the app on your device and the Log messages still appear every six seconds. Here is the entire receiver code :
B4X:
Sub Process_Globals
    
End Sub

'Called when an intent is received.
Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
    Log("Intent received at " & DateTime.Time(DateTime.Now))
    setNextAlarm
End Sub

' Set next alarm six seconds from now
Private Sub setNextAlarm
    StartReceiverAt(Me, DateTime.Now + 6000, False)
End Sub

The process is started in the main activity with a single line of code. The questions that you posed in post #13 are mostly irrelevant. Total project is attached.
 

Attachments

  • Alarm.zip
    9.3 KB · Views: 52
Upvote 0
Top