Android Question Service closes when you lock the screen

Radisk3

Member
Licensed User
Hello,

First of all excuse my ignorance, I am a beginner in B4A.

I will try to explain my problem in detail.

I have an application that works as a calendar.
In the first run it creates the database, searches the data on the web and updates the local database (SQLite). It works 100%.

With the app open or the screen active, it does the calendar event checking routine every 30 seconds on the local database, issuing notifications when needed and every 30 minutes synchronizes with the web. Also 100% operational.

This verification operation is divided into 2 services.
1. ServiceAMF: Check the local database every 30 seconds.
2. SynchronizeAMF: Synchronization routines with the web database.

I call the services when the app starts, in the Starter, through the StartService.
In the Service_Start of the called service, I schedule through the StartServiceAt (Me, DateTime.Now + (30 * 1000), True) in the case of ServicoAMF.
When the service terminates the check routine it is closed through the StopService ("").

The active SmartPhone works normally.
When the screen is locked, the service starts and is terminated by the system.

How do I get the service running all the time?

It can be through the StartService, StartServiceAt or any other routine.
 

Radisk3

Member
Licensed User
Hello Erel,

Thank you for your help.
The app just ran exactly as it should.
However, the notification appears that it is open in the system as you can see in the attached image.

I need it to run, but the notification should not go away. How do I solve this?

Thank you.
 

Attachments

  • app_open.jpeg
    app_open.jpeg
    65.1 KB · Views: 266
Upvote 0

Radisk3

Member
Licensed User
Hello,

Please, be sure that the notification's icon file exists in the \Objects\res\drawable folder

Yes, it is, see attachment Local_icon_AMF.png.
Got it wrong or the file extension is wrong?

AutoCancel should be set to False ?

In the "Starter" I call the service in "Service_Create" like this:
B4X:
StartService ("ServicoAMF")

In the Service "ServicoAMF", in Service_Start, I have:
B4X:
.   If Ntf.IsInitialized=False Then Ntf.Initialize
    Ntf.OnGoingEvent = True
    Ntf.Sound = False
    Ntf.Vibrate = False
    Ntf.Light = True
    Ntf.AutoCancel=False
    Ntf.SetInfo("App AMF em execução","Antônio Meneghetti Faculdade",Main)
    Ntf.Icon = "icon"
    Service.StartForeground(1,Ntf)

Am I doing something wrong?
 

Attachments

  • Local_icon_AMF.png
    Local_icon_AMF.png
    48.8 KB · Views: 216
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Got it wrong or the file extension is wrong?
No that's OK for the file's type. And it is in the right folder. I was only surprised not to see it on the screenshot.
In the "Starter" I call the service in "Service_Create"
That is OK too. But, please in the ServicoAMF do you use the Service.StartForeground(Id, Notification) ? This should put your notification on and the service in the right state. If so, the notification should not disappear

Edit: saw it
 
Upvote 0

Radisk3

Member
Licensed User
I think I understand.
But I would like to leave the service running even when the phone is locked but without notification.
Any idea?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
But I would like to leave the service running even when the phone is locked but without notification.
Running in the background while the device is sleeping is problematic, especially on newer versions of Android.

The best you can do is to have a foreground service with a partial lock.
 
Upvote 0

Radisk3

Member
Licensed User
I am outraged by my ignorance ...

I researched the forum and from what I saw some comment on power consumption using Partial lock.
How do I make a Partial lock correctly?

I wanted to know how Telegram can always be running ...
 
Upvote 0

Radisk3

Member
Licensed User
Thanks for the info Manfred.
Can you help me any more?
I am no longer a programming genius and my ignorance with the Android platform leaves me lost.

How can I use intent filter and push notifications like firebase messages?

Leave the service always running I got it but I do not know to take the notification.
I'm using:
B4X:
.   If Ntf.IsInitialized=False Then   Ntf.Initialize
   Ntf.OnGoingEvent = True
   Ntf.Sound = False
   Ntf.Vibrate = False
   Ntf.Light = True
   Ntf.AutoCancel=False
   Ntf.SetInfo("App AMF em execução","Antônio Meneghetti Faculdade",Main)
   Ntf.Icon = "icon"
   Service.StartForeground(9,Ntf)

I already tried to change the StartForeground number.
With zero it goes without the icon but it also does not work.
Is there any relation of the number to use in the StartForeground?
 
Upvote 0
Top