Android Tutorial Service Modules

LucaMs

Expert
Licensed User
Longtime User
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.


I had left the programming about 10 years ago, when VB.Net was becoming more complex (but not for this reason).
Maybe that's why I do not remember the difference.

However, in B4A classes, you can declare public variables in the routine Class_Globals: as far as I know, they are public and global and I was talking about these (I do not see others ).

More or less this is the reason why I tend also to not use variables at the module level (eg Private in Class_Globals) to be used in the routines but I prefer to pass them as parameters to the routines; in this way, it will be easier to make them "autonomous" and perhaps use them in other modules or libraries.
 
Last edited:

Antony Danby

Member
Licensed User
Longtime User
As I was saying earlier it all boils down to what you need to expose to other classes ( your interface ). As Erel seems to be stating there seems to be a need to have the variables that we need to watch in the Service exposed as public as it looks like accessor methods ( getters and setters ) cannot be used. Is this interpretation correct ? If so, then that's just life, not everything is perfect in life I can deal with close to perfection and work around it.
 

LucaMs

Expert
Licensed User
Longtime User
Some things are not clear to me, but I prefer not to elaborate and accept them as they are

In this case I am referring to a third thing, in addition to what I wrote in my previous post and the response of Erel, which covers references to Context etc. (although I suppose that they are related).

I can directly run a routine of a module from within an Activity:
eg
B4X:
Activity_Resume
MyCodeModule.RoutineX


but I can not if the routine is in a service:
B4X:
Activity_Resume
MyServiceModule.RoutineY


I must use CallSub:
B4X:
Activity_Resume
CallSub(MyServiceModule, "RoutineY")



(different speech for global public variables in my previous post, for which Erel has lost hope that I can understand )
 

Straker

Active Member
Licensed User
Longtime User
Luca, if you think a bit (ok, heavy task) it's obvious.
A service is something separated from the activity. The activity may finish but the service is still running. There is not a real 'link' between them, so you need the callSub statement.
Code modules instead are just addiction all code placed in another file just for have a cleaner code and reusability of the subs.
 

LucaMs

Expert
Licensed User
Longtime User


You're right, often i don't want to think to much, because I need to preserve my beauty

In this case, however, I should also read (two labors at the same time? I risk of becoming Brunetta! ), because I do not remember that a service will remain active after the death of an activity.[EDIT: death of process, because it is clear that you can "finish" an Activity without close your app]

If it were so, means it that they run in separate threads? (Ok, I will read).

Since you do not have these problems (in the sense that you think too much and you lose in a beauty contest against the opponent above mentioned ), would you be kind and clarify my ideas about #140 #141?


Thank you, Cap.
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
If it were so, means it that they run in separate threads? (Ok, I will read).

(I'm reading)
From post #1:
"Code in a service module runs in the same process and the same thread as all other code."


From Android Process and activities life cycle post #1:
"A process has one main thread which is also named the UI thread which lives as long as the process lives. A process can also have more threads which are useful for background tasks."
 
Last edited:

Straker

Active Member
Licensed User
Longtime User
(I'm reading)
From post #1:
"Code in a service module runs in the same process and the same thread as all other code."

Yes, it's true. Is in the same thread but they are two different processes.
For example, you can have and activity that performs some task and a service providing data for that task.
You can call Activity.Finish and the service is till running (if not ended otherwise).

Better example: an app that every tot minutes retrieves data from a webserver with a service. If data is present it will show the data in the Activity's UI. You can kill the activity (I mean completely close, destroy, swipe away from 'task manager') but every tot minutes the service fetch the data (I assume the service will schedule itself every tot minutes with StartServiceAt + time). The service can check if the main activity is in foreground with IsPaused(activityname). If is in pause (or destroyed) the service can pop up a notification (you got a message!) and also prepare the data for activity with a callSubDelayed, and when the user click on the notification the activity is restarted and callSubDelayed can send the data. If is not paused / destroyed, the service just send the data to the activity with a normal callSub.

If you want to play with services, and see what happens if you kill the app but not the service, I posted a simple demo of a socket which every 3 minutes retrieves data from a VB66 socket server (source included) and if the activity is in foreground, shows the data, else performs a notifications. I also added a Text to Speech class. http://www.b4x.com/android/forum/threads/45017/#content
 

Straker

Active 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.

There is a diffference between public and global variables.
A public variable is a variable that you can access everywhere. Public variables is technically forbidden inside a class, because a class should be 'closed' and every value or data the class needs must be passed through a property or method of the class.

A global variable is something you can access everywhere inside a module.
Speaking of class, in VB6 the global variables were the 'mvar...', variables the the class can access everywhere inside the class itself, but which are not exposed outside a class (or exposed only through a Get or Set)

Speaking of class, to be honest, I don't agree with the fact that the variables declared in class_Globals are automatically global, those variables should be automatically Private, not Global (but in B4a all the variables are global if you don't declare them as private, so there is a logic). For example if you have a class Person with a variable Age you can write Person.Age and reference directly the variable. I prefer to mantain all the class variables Private and reference them with Set and Get...
 

Straker

Active Member
Licensed User
Longtime User

I know that there is only one process going on. But you can think the service as a separate process because you can terminate the activity and leave the service running.
 

Straker

Active Member
Licensed User
Longtime User
You're making more confusion than me: an Activity is not the process, it is not listed in the task manager.

Ok, probably I'm missing some correct word. When you write an App, even as simple as Hello World (Just a msgbox in the Activity_Create), how do you call it? For me is an activity...
 

LucaMs

Expert
Licensed User
Longtime User
Speaking of class, in VB6 the global variables were the 'mvar...', variables the the class can access everywhere inside the class itself, but which are not exposed outside a class (or exposed only through a Get or Set)

I call them "Module Variables", not Public Variables.

In fact, in a class you can define a Private variable in the Class_Globals and it will be accessible to all routines of the class.

If you define that variable as Public it will be accessible outside the object! So, these are [EDIT: the only] Global variables, I think.


[P.S. I call them "module variables"? What m in mVar means? Module variables ]
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Ok, probably I'm missing some correct word. When you write an App, even as simple as Hello World (Just a msgbox in the Activity_Create), how do you call it? For me is an activity...

Now I can't, but I'll try this:

Just one Activity (Main!) and a Service.
Run and ExitApplication in the Activity_Resume (after started the Service)

Do you think the Service will be still alive?
 

Straker

Active Member
Licensed User
Longtime User
Last edited:

Straker

Active Member
Licensed User
Longtime User
Ok, the process restarts... so also the Main Activity?

Have you experienced this in your test?

The main activity doesn't restart. It has been finished.
In my test I can leave the service running and kill the activity (activity.finish OR ExitApplication, and also "swipe out" from taskmanager). The service is still running (of course). If the service pops up a notification, from it the O.S. can re-create the activity.
If you want completely end the application you need first to StopService and CancelScheduledService (so the service will stop and it will not be recreated), then you can finish the activity. Now the app is really "dead".

Anyway, I still don't grab the differences between Activity.Finish and ExitApplication... Can someone help and explain?
 

LucaMs

Expert
Licensed User
Longtime User
Anyway, I still don't grab the differences between Activity.Finish and ExitApplication... Can someone help and explain?

Activity.Finish closes the Activity, not the App!

ExitApplication closes the App, but, as wrote in the post you linked, if there is an active service, the process restarts. In this case, if you put a Log in the Main Activity, you could be sure if Main restarts or not (i'm not so sure, i think that Main restarts)
 

Straker

Active Member
Licensed User
Longtime User

You cannot put a log. When the activity finishes the debugger will disconnect and you don't have the logs.
I placed a msgbox in Create and Resume. When the service restart they are not fired up. The are fired only when the activity comes in foreground (for example when 'revived' by a click on a notification...)
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…