Android Question Service running back

santiago

Member
Licensed User
Longtime User
Dear Sir,
I were looking for this question and I found nothing.I beg your pardon if exists an answer to my question.

I develop a application based on your GPS example. It has a Main and a GPS service (starter) As you know the service read GPS location and trigger Main subs.

IN the Main I have a SUB named SAVEGPS .It saves to SQL database the GPS labels (lblLat and lblLon) values.
On service I added a timer and each 30 seconds it tigger the MAIN SUB SAVEGPS

All works fine ... while the application its on front. When I hide application or when the smartphone "sleeps" after a while (screen is off) the service stops or the Main SUB SAVEGPS does not work because its on the Main module ( I am not sure what)

What I must do for it to work on background ? Maybe I must Place all SUBS en service module? (Starter)

Sorry for my English. I am not sure I explained well
Thanks in advance for your time and help.
 

KMatle

Expert
Licensed User
Longtime User
What I must do for it to work on background ?

Put everythig in a service module and use this

B4X:
Sub Service_Create
    sNotif.Initialize
    sNotif.Icon = "icon"
    sNotif.Vibrate=False
    sNotif.SetInfo2("GPSPinger","Service Running","FromNotification",Main)
    sNotif.Sound = False
    sNotif.Notify(1) 
    Service.StartForeground(1,sNotif)
End Sub

This: Service.StartForeground(1,sNotif) will bring your service to run in foreground (which means it will run even if the app is brought to "background"). The notification is needed to inform the user that there is an app running in the background. Works for me (for several hours with screen switched off).

Please check your energy settings, too. On my Huawei I needed to switch on "can run in background" (settings -> apps -> your app -> energy -> can run when screen is off or similar)
 
Upvote 0

santiago

Member
Licensed User
Longtime User
Thanks for your fast response.

I am sure the service is running on background. I wrote a toastmessage and its shown each 10 seconds.

I use callsub to interact with Main functions.

Maybe I must call StartActivity before callSub ?

Or asking in another way ... how can "awake" the Main activity or send it from background to front from the service ??? Its that possible ???

Thanks
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
check if the activity is paused or not.
Use CallSubDelayed if it is paused.
 
Upvote 0

santiago

Member
Licensed User
Longtime User
Thanks

I read about callSubDelayed and its said if activity is on background then the message will be stored in a special message queue. In this case the sub will be called when the target activity becomes visible. The sub will be called before Activity_Resume.

I think I have two choices :

a) Write down the SQL codes on service, not in Main
b) Call the activity .I mean "awake or show" the activity from BACKGROUND . But I do not know how to do this.
 
Upvote 0
Top