Android Tutorial Service Modules

This is an old tutorial. Most of the information here is no longer correct.



Basic4android v1.2 adds support for Service modules.
Service modules play an important role in the application and process life cycle.
Start with this tutorial if you haven't read it before: Android Process and activities life cycle
Code written in an activity module is paused once the activity is not visible.
So by only using activities it is not possible to run any code while your application is not visible.
Services life cycle is (almost) not affected by the current visible activity. This allows you to run tasks in the background.
Services usually use the status bar notifications to interact with the user. Services do not have any other visible elements. Services also cannot show any dialog (except of toast messages).
Note that when an error occurs in a service code you will not see the "Do you want to continue?" dialog. Android's regular "Process has crashed" message will appear instead.

Before delving into the details I would like to say that using services is simpler than it may first sound. In fact for many tasks it is easier to work with a service instead of an activity as a service is not paused and resumed all the time and services are not recreated when the user rotates the screen. There is nothing special with code written in service.
Code in a service module runs in the same process and the same thread as all other code.

It is important to understand how Android chooses which process to kill when it is low on memory (a new process will later be created as needed).
A process can be in one of the three following states:
- Foreground - The user currently sees one of the process activities.
- Background - None of the activities of the process are visible, however there is a started service.
- Paused - There are no visible activities and no started services.

Paused processes are the first to be killed when needed. If there is still not enough memory, background processes will be killed.
Foreground processes will usually not be killed.

As you will soon see a service can also bring a process to the foreground.

Adding a service module is done by choosing Project - Add New Module - Service Module.
The template for new services is:
B4X:
Sub Process_Globals

End Sub

Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub
Sub Process_Globals is the place to declare the service global variables. There is no other Globals sub like in Activity as Service doesn't support Activity objects.
Sub process globals should only be used to declare variables. It should not run any other code as it might fail. This is true for other modules as well.
Note that Process_Global variables are kept as long as the process runs and are accessible from other modules.

Sub Service_Create is called when the service is first started. This is the place to initialize and set the process global variables. Once a service is started it stays alive until you call StopService or until the whole process is destroyed.
Sub Service_Start is called each time you call StartService (or StartServiceAt). When this subs runs the process is moved to the foreground state. Which means that the OS will not kill your process until this sub finishes running. If you want to run some code every couple of minutes / hours you should schedule the next task with StartServiceAt inside this sub.

Sub Service_Destroy is called when you call StopService. The service will not be running after this sub until you call StartService again (which will run Sub Service_Create followed by Sub Service_Start).
Service use cases

As I see it there are four main use cases for services.
- Separating UI code with "business" or logic code. Writing the non-UI code in a service is easier than implementing it inside an Activity module as the service is not paused and resumed and it is usually will not be recreated (like an Activity).
You can call StartService during Activity_Create and from now on work with the service module.
A good design is to make the activity fetch the required data from the service in Sub Activity_Resume. The activity can fetch data stored in a process global variable or it can call a service Sub with CallSub method.

- Running a long operation. For example downloading a large file from the internet. In this case you can call Service.StartForeground (from the service module). This will move your activity to the foreground state and will make sure that the OS doesn't kill it. Make sure to eventually call Service.StopForeground.

- Scheduling a repeating task. By calling StartServiceAt you can schedule your service to run at a specific time. You can call StartServiceAt in Sub Service_Start to schedule the next time and create a repeating task (for example a task that checks for updates every couple of minutes).

- Run a service after boot. By setting the #StartAtBoot attribute to True, our service will run after boot is completed.
Notifications

Status bar notifications can be displayed by activities and services.
Usually services use notifications to interact with the user. The notification displays an icon in the status bar. When the user pulls the status bar they see the notification message.

Example of a notification (using the default icon):

notification_1.png



notification_2.png


The user can press on the message, which will open an activity as configured by the Notification object.

The notification icon is an image file which you should manually put in the following folder: <project folder>\Object\res\drawable.
Accessing other modules

Process global objects are public and can be accessed from other modules.
Using CallSub method you can also call a sub in a different module.
It is however limited to non-paused modules. This means that one activity can never access a sub of a different activity as there could only be one running activity.
However an activity can access a running service and a service can access a running activity.
Note that if the target component is paused then an empty string returns.
No exception is thrown.
You can use IsPause to check if the target module is paused.

For example if a service has downloaded some new information it can call:
B4X:
CallSub(Main, "RefreshData")
If the Main activity is running it will fetch the data from the service process global variables and will update the display.
It is also possible to pass the new information to the activity sub. However it is better to keep the information as a process global variable. This allows the activity to call RefreshData whenever it want and fetch the information (as the activity might be paused when the new information arrived).

Note that it is not possible to use CallSub to access subs of a Code module.

Examples:
Downloading a file using a service module
Periodically checking Twitter feeds
 
Last edited:

entolium

Member
Licensed User
Longtime User
When I press the power button, and the screen turn on, the Notify is running and showing the message "Device connected".
Is possible the problem could be the astreams?
 

entolium

Member
Licensed User
Longtime User
Thank you Erel,

This is the code:

B4X:
Sub AStreams_Error
    astreams.Close
    StartServiceAt("", DateTime.Now + 2 * DateTime.TicksPerSecond, True) 'schedule the next task to run in 2 seconds.
End Sub

Sub Astreams_Terminated
    astreams.Close
    StartServiceAt("", DateTime.Now + 2 * DateTime.TicksPerSecond, True) 'schedule the next task to run in 2 seconds.
End Sub

The objetive is use a tablet like car GPS, with an usb antena. The gps never must fail, and must recover it in any error.
I'm trying to restart the service in all errors and when power on, but nothing works.
 

entolium

Member
Licensed User
Longtime User
Thank you Erel,

I can't debug the app. The usb is used by the gps and througt wifi I have problems and the debugger stops.

When power off, the GPS antenna power off (the led off). I add toast message in all lines, and I think the only one problem is the connection usb breaks (the astream don't fire any error).
With a Timer I could monitor the usb state, but I need the usb connection state property for check, and the library don't have it :(.
 

entolium

Member
Licensed User
Longtime User
More questions, I'm sorry.

I have one activity and one service.

When execute "StartService(..." (Pressing F8), approximately 12 seconds after (the yellow bar is waiting in the line "Sub Service_Create"), whitout touching anything, the service run the code o_O

What is happening?
 

entolium

Member
Licensed User
Longtime User
Please start a new thread for this. There might be other ways to find that the USB has disconnected.

I'm resolved. I added "Startservice" in Activity_Resume. My app is linked with the usb device, then, when the system return from deep, resume the app and the service start again.
 

MrKim

Well-Known Member
Licensed User
Longtime User
IF I start a service and then open a MsgBox will the service continue to run?
 

incendio

Well-Known Member
Licensed User
Longtime User
I wonder about the use of Service.

Suppose I have a service to download a lot of data from internet at the specific time, since service is run in the same thread with other activities, it will freeze user interaction when service is running, right?
 

LucaMs

Expert
Licensed User
Longtime User
I would say wrong. One of the reasons to using the services is to delegate them long works to execute in background.

As Erel says in the main post:
- Running a long operation. For example downloading a large file from the internet. In this case you can call Service.StartForeground (from the service module). This will move your activity to the foreground state and will make sure that the OS doesn't kill it. Make sure to eventually call Service.StopForeground.

The first example, moreover, does just that.
 

incendio

Well-Known Member
Licensed User
Longtime User
Even running on the same thread, it won't interrupt current process?

Isn't it, when running on the same thread all processes must be carried in order, mean you can't do next process if previous process not finished yet.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Suppose I have a service to download a lot of data from internet at the specific time, since service is run in the same thread with other activities, it will freeze user interaction when service is running, right?
No. All the libraries that handle network communication (including HttpUtils2) make sure to use background threads for the communication.
 

incendio

Well-Known Member
Licensed User
Longtime User
OK, but after download finished, service will do something with this data, before presented to users, may be doing some calculations, saving to database, etc.

This process could take times, and before the process finished, won't it delayed other process?
 

incendio

Well-Known Member
Licensed User
Longtime User
It is not about CPU, it's about thread.

Even there are more CPU, if running on the same thread, I think it could delayed other process.

The solution that I can think right now is separate main application with service app, but I don't know if service app able to read or right main app data if this data located on directory internal.
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
You wrote "delayed" not interrupt.

Since I'm starting now to use the service modules, I'm not sure.

The previous Erel's answer suggests that the communication takes place in a different thread, but it is not clear if ONLY for communication.

But there is an optimal solution: try it!

;)
 
Top