Android Tutorial Service Modules

incendio

Well-Known Member
Licensed User
Longtime User
Yeah, I mean delayed.

The solution still need service in main app to regularly check finished tasks on service app.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The code you write in the service is executed in the main thread.

If you did encounter any performance issues then you should post more specific details about your implementation. SQL library does support asynchronous operations. So if it is slowing down your app (and you are correctly using transactions) then you should use these methods.
 

incendio

Well-Known Member
Licensed User
Longtime User
I have use sqlite in memory and transaction,but not asynchronous operations.
During an insert operation (about 20K records) and some calc, there was a delayed in UI about 3-4 secs. Will try asynchronous operations and see what happens.
 

incendio

Well-Known Member
Licensed User
Longtime User
Single transaction? What do you mean?

I wrote my code like this
Begin transaction
do some insert
do some calc/other proc
end transaction
 

incendio

Well-Known Member
Licensed User
Longtime User
Yes I did the loops for .. next inside the insert operation.

3-4 seconds is depend, when it is idle or users waiting for some more long operation is ok, but when in the middle of doing some input, is a bit disturbing, especially when you need to re-download data again.

Wouldn't mind at all if in the future update, B4A has a feature to running service in its own thread
 

Troberg

Well-Known Member
Licensed User
Longtime User
I've made a media player with a service as the player back end. Works nicely as long as the front end is visible, but if another activity is visible or the screen is off, it stops playing after a track is complete. The Complete event for the media player fires just fine, and in it, I call the activity using this code:

B4X:
CallSubDelayed(Main,"MP_Complete")

In my mind, it should start the Main activity if needed, then call MP_Complete. However nothing happens.

The log says this:

sending message to waiting queue (CallSubDelayed - MP_Complete)
running waiting messages (1)

Then, nothing happens.

If I manually bring the activity to the front again, the code continues as expected.

Am I doing something wrong here? What is the preferred method to call an activity from a service?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
See the tutorial: Using CallSubDelayed to interact between activities and services

CallSubDelayed will not start the activity if the app is in the background. Why do you need to start the activity at all? It is not recommended to start an activity from a service when the app is in the background as the user is doing something else.
 

Troberg

Well-Known Member
Licensed User
Longtime User
Well, it's kind of a "supposed to always be at the front"-app.

Anyway, I'll just have to rethink my code a bit, and move more stuff to the service.
 

Antony Danby

Member
Licensed User
Longtime User
Erel

Its not clear in any of the documentation anywhere, but appears as if I cannot declare and use a class instance within a service is this true ?
My error at compilation was:

Error compiling program
Error description: Cannot access activity object from sub
Process_Globals
Occurred on line: 12
Dim control As MainControl
Word: maincontrol

If this is true it wrecks my whole design.

I wanted to do: Dim control As MainControl within the Service's Process_Globals ( where MainControl was my controller which was itself looking the results from other instantiated classes ) and then do control.Initialize(Me,"States_Changed") and have the result from listening to the other classes come back into a single event ( States_Changed ). It's a simple controller design and should work.

However, if this is not true I don't even know how to do what I need to do and what really would be the point of a service if you can't invoke this kind of design.?
 

Antony Danby

Member
Licensed User
Longtime User
No.

However if your class has a global variable which requires an activity context (like a View) then you cannot use it from a service.

Ok

This is what I thought; my MainControl is a class and is a "black box" and contains no variables or calls to any View.
Oh; hang on. I have one in my lower levels; ah bad design ;-)

Thanks for your quick response and good reply Erel.

I knew my design wasn't wrong ( Delphi coder for a long time ) and i did think B4A would be capable of it.
Just a B4A newbie mistake !
 

Antony Danby

Member
Licensed User
Longtime User
Maybe I'm wrong, but I think that the classes should not contain global variables. It is better pass the value using the properties.

No disagreement from me on that point.
I try to use private variables and only expose to the interface those variables that I need for other classes.
The whole secret behind designing good classes hangs on what you expose to the outside world and what you don't
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Maybe I'm wrong, but I think that the classes should not contain global variables.
I think that you are confusing public variables with global variables. While personally I use both some consider public variables to be problematic.

Global variables (instance variables) however are very important as they store the instance state.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…