Android Question Activity recognition stops working when closing the app

Discussion in 'Android Questions' started by Almog, Aug 7, 2019.

  1. Almog

    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
     
  2. MarkusR

    MarkusR Well-Known Member Licensed User

    try to start a extra foreground service that will stay.
     
    Almog likes this.
  3. Almog

    Almog Active Member Licensed User

    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:
    Code:
    Sub Service_Create
        
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
    End Sub
    (Although the foreground service unremovable notification has shown)
    Thanks in advance
     
  4. MarkusR

    MarkusR Well-Known Member Licensed User

    i used it this way.
    (its not the starter service)
    Code:
    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
     
    Almog likes this.
  5. Almog

    Almog Active Member Licensed User

    Thank you very much!
    So it's in the 'RecognitionService', right?

    Thanks in advance
     
  6. MarkusR

    MarkusR Well-Known Member Licensed User

    if you put your logic there then yes.
    the notify click will open the activity used here as argument ",Main)
     
    Last edited: Aug 7, 2019
    Almog likes this.
  7. Almog

    Almog Active Member Licensed User

    OK, thank you very much! I'll try
     
  8. MarkusR

    MarkusR Well-Known Member Licensed User

    ups i mixed the answer with other thread. forget this part with camera.
     
  9. Almog

    Almog Active Member Licensed User

    I tried it but still, it worked only for the first time, and after closing the app it doesn't notify anymore :(
     
  10. MarkusR

    MarkusR Well-Known Member Licensed 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.
     
  11. Almog

    Almog Active Member Licensed User

    Yes, I have it too, but it doesn't continue to notify about the user's physical activity...
     
  12. MarkusR

    MarkusR Well-Known Member Licensed User

    as long as the notify is visible the service should run.

    i started my service from starter service
    Code:
    Sub Service_Start(StartingIntent As Intent)
       
        
    Log("Starter.Service_Start")
           
        
    StartService(Connection)
       
    End Sub
    and my service used at top
    Code:
    #Region  Service Attributes
        
    #StartAtBoot: True
       
    #End Region
    you can log this events , replace Connection with your service name
    Code:
    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
     
    Almog likes this.
  13. Almog

    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...
     
  14. MarkusR

    MarkusR Well-Known Member Licensed 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.
     
    Almog likes this.
  15. Almog

    Almog Active Member Licensed User

    I used release mode..

    I got this error when the app crashed:
    Code:
    --------- 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:
    Code:
    *** 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...
     
  16. Almog

    Almog Active Member Licensed User

    After closing the app and opening again, the notifications about activity recognition stops notifying
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    The error message that you posted can only happen in debug mode.

    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.
     
    Almog likes this.
  18. Almog

    Almog Active Member Licensed 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
     
  19. MarkusR

    MarkusR Well-Known 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?

    Code:
    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
     
    Almog likes this.
  20. DonManfred

    DonManfred Expert Licensed 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.
     
    Last edited: Aug 8, 2019
    Almog likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice