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
try to start a extra foreground service that will stay.
 
Upvote 0

Almog

Active Member
Licensed User
try to start a extra foreground service that will stay.
Thanks! I tried to do that, but it still didn't work (After closing the app, it stopped from getting reports).
Here is what I added:
B4X:
Sub Service_Create
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
End Sub
(Although the foreground service unremovable notification has shown)
Thanks in advance
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
i used it this way.
(its not the starter service)
B4X:
Sub Service_Start(StartingIntent As Intent)
   
    Dim n As Notification
    n.Initialize
    n.Icon="icon"
    n.Sound=False
    n.Light = False
    n.Vibrate = False
    n.SetInfo("Alarm","Listening for a Alarm Message",Main)
    Service.StartForeground(1,n)

    PhoneWakeState1.PartialLock
        
End Sub
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
Thank you very much!
So it's in the 'RecognitionService', right?
Thanks in advance
if you put your logic there then yes.
the notify click will open the activity used here as argument ",Main)
 
Last edited:
Upvote 0

Almog

Active Member
Licensed User
if you put your logic there then yes.
the notify click will open the activity used here as argument ",Main)
in this activity event (create / resume) you can start your intent for the camera app.
OK, thank you very much! I'll try
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
ups i mixed the answer with other thread. forget this part with camera.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
for me if me drag out the app from task list the notify will stay at top of my phone and if me click it the activity opens.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
as long as the notify is visible the service should run.

i started my service from starter service
B4X:
Sub Service_Start(StartingIntent As Intent)
   
    Log("Starter.Service_Start")
       
    StartService(Connection)
   
End Sub

and my service used at top
B4X:
#Region  Service Attributes
    #StartAtBoot: True
   
#End Region

you can log this events , replace Connection with your service name
B4X:
Sub Service_Create

    Log("Connection.Service_Create")
    
End Sub

Sub Service_Start(StartingIntent As Intent)
    
    Log("Connection.Service_Start")
    
    

    
End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.

    Log("Connection.Service_TaskRemoved")
    

End Sub

Sub Service_Destroy

    Log("Connection.Service_Destroy")

End Sub
 
Upvote 0

Almog

Active Member
Licensed User
as long as the notify is visible the service should run.

i started my service from starter service
B4X:
Sub Service_Start(StartingIntent As Intent)
  
    Log("Starter.Service_Start")
      
    StartService(Connection)
  
End Sub

and my service used at top
B4X:
#Region  Service Attributes
    #StartAtBoot: True
  
#End Region

you can log this events , replace Connection with your service name
B4X:
Sub Service_Create

    Log("Connection.Service_Create")
   
End Sub

Sub Service_Start(StartingIntent As Intent)
   
    Log("Connection.Service_Start")
   
   

   
End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.

    Log("Connection.Service_TaskRemoved")
   

End Sub

Sub Service_Destroy

    Log("Connection.Service_Destroy")

End Sub
Thank you! I tried it now, but the app crashed...

I think that in this case (ActivityRecognition service) this service is started automatically from the main Activity...
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
you started the project in debug mode from ide?
put some break points or logs.

activitys (windows) get lost if you drag your app out of task list, services not.
 
Upvote 0

Almog

Active Member
Licensed User
Thank you! I tried it now, but the app crashed...

I think that in this case (ActivityRecognition service) this service is started automatically from the main Activity...
you started the project in debug mode from ide?
put some break points or logs.

activitys (windows) get lost if you drag your app out of task list, services not.
I used release mode..

I got this error when the app crashed:
B4X:
--------- beginning of main
--------- beginning of crash
java.lang.RuntimeException: Unable to create service b4a.example.starter: java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[])' on a null object reference
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3556)
    at android.app.ActivityThread.access$1300(ActivityThread.java:200)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6762)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[])' on a null object reference
    at anywheresoftware.b4a.shell.Shell.virtualAssets(Shell.java:164)
    at anywheresoftware.b4a.shell.Shell.start(Shell.java:102)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:92)
    at b4a.example.starter.onCreate(starter.java:34)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3544)
    ... 8 more
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[])' on a null object reference
    at anywheresoftware.b4a.shell.ShellConnector.sendControlMessage(ShellConnector.java:61)
    at anywheresoftware.b4a.shell.Shell.virtualAssets(Shell.java:124)
    ... 12 more

And this in the second running and closing the app, after adding logs:
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (recognitionservice) Create ***
Connection.Service_Create
** Service (recognitionservice) Start **
Connection.Service_Start
** Service (recognitionservice) Start **
Connection.Service_Start
** Activity (main) Pause, UserClosed = false **

I didn't see any activity recognition notifications after closing the app with the task list, although the service still exists as the log shows...
 
Upvote 0

Almog

Active Member
Licensed User
After closing the app and opening again, the notifications about activity recognition stops notifying
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I used release mode..

I got this error when the app crashed:
The error message that you posted can only happen in debug mode.

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?
Killing the app by swiping it from the recent apps list is not the same as "closing" the app. On some devices the app will not be able to start after it was explicitly killed by the user.
 
Upvote 0

Almog

Active Member
Licensed User
Killing the app by swiping it from the recent apps list is not the same as "closing" the app. On some devices the app will not be able to start after it was explicitly killed by the user.
OK, Thank you, but shouldn't the activity recognition service itself work even when the app is killed? (Or the service stops after the app is killed?)

Thanks in advance
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime 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
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
recognition service itself work even when the app is killed?
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.
 
Last edited:
Upvote 0
Top