Android Question Services

FFMan

Member
Licensed User
Longtime User
I am trying to write a background service that retrieves data from the internet, transforms it and then sends it via Bluetooth. I have this working as an activity and want to convert it to a service.

I have looked at the tutorial and examples and created a small test case but my code has a main module and a service module and I expected only to have a service module. Is this the way to launch the service, from the activity, then once the service is launched can I kill the activity process ?

I guess I'm wondering if I can create a service only module, that has an app icon I can click on to launch, or is this not possible/recommended ?
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hello FFMan,
First of all you are on the good way but remember that your service can be launched :
- or from the main activity's code (for example if you press a button or when the Activity_Resume's sub is reached
- or at boot (when the phone starts)
- or by itself (StartServiceAt)

So, if you want to have a service with an icon in the launcher, you only have to start your service from the Main activity. When the Main's Activity_Resume sub is reached, then if the service is not already running, start it :)

Nothing is impossible or not recommended till you remember how the service works :
- when it is first started, it passes the Service_Create sub and after that the Service_Start
- when it is already running, you can call many times the Service_Start sub
- if you need a special action to be done, you can start the service and call the special sub using Callsubdelayed(Service_name, "Sub")

That's all :)
 
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
on the main

Sub lala
ExitApplication
End Sub


on the service

CallSubDelayed(main,"lala")
 
Upvote 0

FFMan

Member
Licensed User
Longtime User
thanks for the help

how can I tell in my activity if the service is already running. I could use a global variable set in the service but I guess there is a better way ?
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
how can I tell in my activity if the service is already running
From your activity (Main or other or even a service), you can check by code if the service is already running
B4X:
If Not(IsPaused(Your_Service_Name)) Then
  DoSomething
Else
  DoOtherThing
End if
 
Upvote 0
Top