Android Question [B4APages] About background tasks and services

vfafou

Well-Known Member
Licensed User
Longtime User
Hello!

I would like to ask the following:
As I understand, I could have the Starter service for background tasks like push messages functionality with web sockets, send/receive messages via the websocket push framework and background GPS management to send location data to server even if the app is in "background". I also need to "wake" the app from "background" when receiving specific messages.
Things I need to clarify:

1. Do I really need a background service in order to keep the app alive when in background?

2. If yes, should I remove the following line from Starter service, in order to prevent the process being killed from Android?
B4X:
Service.StopAutomaticForeground

And do I need StartServiceAt?

3. Could I use notifications like any other app with background service running? If the service is not needed then how notifications are implemented?

4. How do I "wake" the app from "background"? By now I did the following inside the background service running as foreground service:
B4X:
If msg.type = "ET" then
    If IsPaused(Main) Then
        StartActivity(Main)
    End If
End If
With B4XPages, what is the equivalent?

Thank you in advance!
 

vfafou

Well-Known Member
Licensed User
Longtime User
A B4XPages app isn't much different from a normal B4A app. It is just a single Main activity that displays different panels with different layouts instead of housing those layouts in different activities. Your code above should work - did you try it?
Hello @agraham!
Thank you for your response!
No, I haven't tried it yet because I'm writing the app from scratch, in order to tidy up some things and write more clean code without unnecessary libs and code rows...
I say this time I will do it as clean as possible!:)
OK for trying with the current logic but if it isn't needed to have a service to do the job, it would be a benefit for my code flow!
So my main question if it is needed to have a service for background tasks remains!
;)
 
Last edited:
Upvote 0

vfafou

Well-Known Member
Licensed User
Longtime User
Thank you very much!
A last question:
Assuming that It is just a single Main activity that displays different panels with different layouts, how it doesn't pause when it's in background? It's a little bit confusing!
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Assuming that It is just a single Main activity that displays different panels with different layouts, how it doesn't pause when it's in background? It's a little bit confusing!
If your app is in Background then the activity is paused.
 
Upvote 0

vfafou

Well-Known Member
Licensed User
Longtime User
Yes, but I don't think you can use the Starter service for this. There are lots of posts about this in the forum - try a search.
Reading the post https://www.b4x.com/android/forum/t...framework-for-managing-multiple-pages.118901/, Erel says:
13. There is a field named B4XPages.B4XGlobalContext. The use case behind it is with a class instance that is initialized in Service_Create of the starter service. This can be a good place to implement tasks that can also run in the background.
Doesn't he mean that Starter service is a good place to implement background tasks?
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Found it! In B4xPagesManager.Initialize
B4X:
    Dim jo As JavaObject = Me
    Dim module As JavaObject
    module.InitializeStatic(jo.RunMethodJO("getActivityBA", Null).GetField("className")).SetField("dontPause", True)
I have to admit ignorance as to the reason for, and implications of, this.
 
Upvote 0

vfafou

Well-Known Member
Licensed User
Longtime User
Found it! In B4xPagesManager.Initialize
B4X:
    Dim jo As JavaObject = Me
    Dim module As JavaObject
    module.InitializeStatic(jo.RunMethodJO("getActivityBA", Null).GetField("className")).SetField("dontPause", True)
I have to admit ignorance as to the reason for, and implications of, this.
WOW! So what does it mean? That activity REALLY doesn't pause?
And then background service is not necessary?
I have to say that I didn't think to look at the library code!!! o_Oo_Oo_O
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
WOW! So what does it mean? That activity REALLY doesn't pause?
I don't know what it does as it is not an Android feature but one implemented in B4A. I've traced it through in a compiled apk but still don't understand its purpose. I think only Erel can enlighten us on this.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
All I can see is this
12. The pages context will not be paused or destroyed, until the process itself is killed.
Which doesn't really explain what is happening and the consequence of the Activity not pausing - like what is it doing if it is not paused but also not displayed? For example, are Timers still running?
 
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
Everything is continuing to run exactly as before. Timers run. Sleep calls will be resumed and so on.
Exactly like in B4J and B4i.
Hmm.. so this the major difference between activity-apk and pages-apk, pausing, which I have learnt now finally.
Good that I will forget it in pages.

I request @klaus to complete his booklet on pages now, as it helps me more in understanding things than videos, which makes it unreadable in my small monitor.

Regards,

Anand
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Everything is continuing to run exactly as before. Timers run. Sleep calls will be resumed and so on.
I didn't realise that until just now. Out of interest how is it done? I didn't think that Android allowed Activity code to run in the background!
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Out of interest how is it done?
Android does allow it. You need to make sure not to reference destroyed activities. In the case of B4XPages it is simple as there is only a single activity and it is never destroyed.

Hmm.. so this the major difference between activity-apk and pages-apk, pausing, which I have learnt now finally.
This is not the main difference. Practically there are many differences and all of them are listed here: https://www.b4x.com/android/forum/threads/b4x-b4xpages-what-exactly-does-it-solve.119078/#content
 
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
This is not the main difference. Practically there are many differences and all of them are listed here: https://www.b4x.com/android/forum/threads/b4x-b4xpages-what-exactly-does-it-solve.119078/#content
Yes I read that, but did not thought " The page classes are never paused. " means the activity itself is never paused, which I understood now and also understand why it is similar to B4J (I do not use or have idea of B4I).

B4J is a desktop application and I develop desktop applications daily in my professional work. Now my thinking and planning will be same with B4XPages. Thanks a lot for it.

My fingers are twirling to convert my activity-app to pages-app. Just that I need the knowledge guide of @klaus now to push myself.

@klaus , please do not get me wrong as I have tagged you more recently, but you did helped me more than anyone (even @Erel , sorry) in getting the best grasp of B4X.

Looking forward to reading your latest guide soon.

Regards,

Anand
 
Upvote 0
Top