Android Question Service Start/Stop

aaronk

Well-Known Member
Licensed User
Longtime User
Hello,

If I was to use a service, and in that service I created the following:

Service1:
B4X:
'Service module
Sub Process_Globals
	Dim test1 As String
End Sub


Sub Service_Create
End Sub


Sub Service_Start (StartingIntent As Intent)
End Sub


Sub Service_Destroy
	Log("goodbye")
End Sub

And from a Activity I had a few buttons..
B4X:
Sub Button1_Click
	StartService(Service1)
End Sub

Sub Button2_Click
	StopService(Service1)
End Sub

Sub Button3_Click
	Service1.test1 = "hello"
End Sub

When I press button 1, it will start the service.
Then press button 3 to change the value of the string and then I press button 2 to stop the service.

If I then start the service again with button 1 should the test1 string in the service still be set to 'hello' or will it kill the complete service and all values in the service be back to default when I start it again (this all done with the app open) ?
 

barx

Well-Known Member
Licensed User
Longtime User
If the app is left open, the value should persist.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
How do you Destroy the service completely so it doesn't keep any of the value and loads then back as default?
Do I have to create a sub that I have to call when creating the service to load all default values back in?

What if you where calling a Class from the service, how do you close the service and re-Initialize the class again?
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Process global variables are kept as long as the process is running. You can "clean" them by setting their value to an empty string (or other default value).
Just so I understand a little better, when you say 'are kept as long as the process is running' do you mean while the service is Started or while the app is open.
What if the service is closed/Stopped (as the user pressed button 2 from the above code) does that mean the process is now closed ? or is the process closed when they press the home button on there phone?

Maybe it's worth clearing all data etc. from the service when the Service_Destroy sub is called and when the service is then started again re-load the data using the Service_Start sub ?
Would I be correct in doing that?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I recommend you to go over this tutorial: Android Process and activities life cycle

does that mean the process is now closed ? or is the process closed when they press the home button on there phone?
No.

When your app is in the background the OS can kill the process.

Maybe it's worth clearing all data etc. from the service when the Service_Destroy sub is called and when the service is then started again re-load the data using the Service_Start sub ?
Another important tutorial: Service Modules
You should load the data in Service_Create not Service_Start.

It is up to you to decide what to load and what to reset. There is no reason to force you to reset the already loaded variables.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Thanks Erel..
I had a quick read at both of those tutorials and by reading just some parts of it made me understand a little better what I need to do.
I will have a full read over them tomorrow.. Thanks heaps for your help.
 
Upvote 0
Top