Android Question How? Synchronizing services module life to the life of the app

Lee Gillie CCP

Active Member
Licensed User
Longtime User
I moved some code to monitor LogCat into a service. First time for this for me. I read the tutorial.

I ONLY need this running while my app/process is running. It may not continue to be in the main activity, but some secondary activity, but so long as the process for this app is active (for any activity of it) I want my logcat monitor service to continue to run.

In the Activity_Create of the main/startup activity of the app I call StartService to start my service module.

It escapes me to find an appropriate place to call StopService to shut it down. I do NOT want to do it when the main activity is paused of course.

One thought that occurs to me is that this service is activated to work via an event each time there is logcat activity. Perhaps my service can check and see if the app is still running?? My questions:

1) Is there a way for a service module to see if my app process is still running?

2) Is there a way for a service module to end itself if the results of 1 is NO?

Maybe there is a better way to do this?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Service code
B4X:
Sub Process_Globals
   Dim activities As List   
End Sub

Sub Service_Create
   activities = Array(Main, Activity2) 'list all the activities here
End Sub

Sub Service_Start (StartingIntent As Intent)
End Sub

Sub IsAnyActivityRunning As Boolean
   For Each module As Object In activities
     If IsPaused(module) = False Then Return True
   Next
   Return False
End Sub

Note that activities are considered paused inside Activity_Create sub. This shouldn't matter unless you show a msgbox when an activity is created.
 
Upvote 0
Top