Android Question Activity recognition stops working when closing the app

Almog

Active Member
Licensed User
Hi there!

I tried to build a sample app that notifies (using nb6) when the current user physical activity changes (using physical-activity-recognition-detection library).

It worked, but after closing the app with the user's 'task manager' (where you 'swipe' apps to stop them), I stopped getting notifications about the current user physical activity...

Does anybody know why does it happen? Is there a way to improve it to work even when the app is closed?

Thanks in advance
 

MarkusR

Well-Known Member
Licensed User
Longtime User
When a app is killed then all services of this app are killed also as the services are part of the process. The process is killed.
.
The answer is NO.

after pressung □ and swipe out my app a extra foreground service will stay. i can get his data from process global after i start the app again. at least with my android 8.x device.
 
Upvote 0

Almog

Active Member
Licensed User
if you have the same code as the example link at top
i think the problem is here "If FirstTime Then"
if you turn your device the activity get pause and stop the recognition and if the activity will be recreated it did not start again.
did you put this code somewhere else?

B4X:
Sub Process_Globals
   Public ar As ActivityRecognition
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
    Log("Activity_Create")
    If FirstTime Then
        Log("FirstTime")
        ar.Initialize("ar")
     ar.Connect(5000) 'set the detection interval to 5 seconds.
   End If
End Sub

Sub ar_Connected (Success As Boolean)
    Log("ar_Connected: " & Success)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    Log("Activity_Pause")
    Log("ar.Stop")
    ar.Stop
End Sub
On my code Activity_Pause is empty, so the ActivityRecognition will not stop when exiting the app..
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
but it can happen that android remove the Activitys, its like ms windows would close your app windows.

this object +Initialize +Events should move from the activity to your extra foreground service.
B4X:
 Public ar As ActivityRecognition
 
Upvote 0

Almog

Active Member
Licensed User
but it can happen that android remove the Activitys, its like ms windows would close your app windows.

this object +Initialize +Events should move from the activity to your extra foreground service.
B4X:
 Public ar As ActivityRecognition
As you said, I moved:
B4X:
Public ar As ActivityRecognition
and
B4X:
ar.Initialize("ar")
ar.Connect(5000)
to the RecognitionService service, and it worked! Thank you very much!

I now understand that when Main activity is closed, 'ar' object does not exist anymore, that's why you told me to move it :)
 
Upvote 0
Top