B4A Library Physical Activity Recognition Detection

Discussion in 'Additional libraries, classes and official updates' started by Erel, Jul 1, 2014.

Thread Status:
Not open for further replies.
  1. Erel

    Erel Administrator Staff Member Licensed User

    This library allows you to monitor the user / device physical state (walking, running, still, etc.).

    The state detection is based on Android location services which use low power sensors to try to detect the current activity. Once you connect your app to these services you will receive notifications even when your app is in the background (similar to static intent filters).

    Configuration steps:
    1. Add this line to the main activity:
    Code:
    #AdditionalJar: com.google.android.gms:play-services-location
    2. Add Google Play Services base: https://www.b4x.com/android/forum/threads/integrating-firebase-services.67692/#content
    3. Add this code to the manifest editor:
    Code:
    AddApplicationText(<service
      android:name=
    "anywheresoftware.b4a.objects.ActivityRecognition$RecognitionService">
      </
    service>
      )
    Note that no #AdditionalRes is needed.


    Recognition Service

    Add a service named RecognitionService (must be this name) to your project. This service will receive the activity detection notifications.
    The code should be similar to:
    Code:
    Sub Process_Globals

    End Sub
    Sub Service_Create

    End Sub

    Sub Service_Start (StartingIntent As Intent)
       
    If StartingIntent.Action = "activity_recognition" Then
         
    Dim types As List = StartingIntent.GetExtra("types")
         
    Dim confs As List = StartingIntent.GetExtra("confidence_values")
         
    Log("Current activity: " & types.Get(0))
       
    End If
    End Sub
    The service will receive intents with the detection information. The intent includes two lists with the possible states and confidence levels. Both lists are sorted from the most probable activity to the least probably activity.

    The detected activities are: "in_vehicle", "on_bicycle", "on_foot", "still", "unknown", "tilting", "running" and "walking".

    The confidence level is a number between 0 to 100.

    Connecting to the detection service

    You should initialize an ActivityRecognition object and call connect:
    Code:
    Sub Process_Globals
       
    Public ar As ActivityRecognition
    End Sub

    Sub Globals

    End Sub

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

    Sub ar_Connected (Success As Boolean)
       
    Log("Connected: " & Success)
    End Sub
    Call ar.Stop to stop receiving notifications.

    Notes

    - Android may kill the process and then recreate it when a notification is delivered. This means that it is better to run the app in Release mode. Otherwise it will fail when the process is recreated.

    History

    V2.10 is released. Compatible with Google Play Services v27+
    V2.00 is released. It is compatible with the latest versions of Google Play Services.
     

    Attached Files:

    Last edited: Mar 1, 2017
    toby, wonder, Daniel-White and 16 others like this.
  2. GMan

    GMan Well-Known Member Licensed User

    Hoi Erel,
    what is the difference between on foot and walking ?
     
  3. udg

    udg Expert Licensed User

    Hi GMan,

    it seems there's no such a big difference..
    Read here.

    Umberto
     
    Daniel-White likes this.
  4. Ratna Fang

    Ratna Fang Member Licensed User

    you came just exactly when i need at most :)
    thanks, erel.
    i'll give it a try tonight for sure
    :beer:
     
  5. FreeWolF

    FreeWolF Active Member Licensed User

    Excuse me but I can't find in the Android SDK Manager the download for the play services....Where is located?
     
  6. GMan

    GMan Well-Known Member Licensed User

    Gracie
     
  7. Ratna Fang

    Ratna Fang Member Licensed User

    example:
    E:\androidsdk\adt-bundle-windows-x86_64\adt-bundle-windows\sdk\extras\google\google_play_services\libproject\google-play-services_lib\libs
    where E:\androidsdk\adt-bundle-windows-x86_64\adt-bundle-windows is your SDK directory.

    *you should be easier to find out the /sdk\extras\google\google_play_services\libproject\google-play-services_lib\libs folder from there :)
     
  8. FreeWolF

    FreeWolF Active Member Licensed User

    Ok, thank you very much!!! :)
     
  9. NJDude

    NJDude Expert Licensed User

    Does the interval has any effect? (ar.Connect(5000)) it seems to me that no matter what value is entered the detection time is always slow.
     
    Daniel-White likes this.
  10. Erel

    Erel Administrator Staff Member Licensed User

    Daniel-White likes this.
  11. MaxApps

    MaxApps Active Member Licensed User

    Hi

    How do I get the values, received in the service module, to the activity module?

    Kind regards
    Jakob
     
  12. NJDude

    NJDude Expert Licensed User

    The attached sample uses this new library and displays the results on an activity, when the HOME key it pressed, the results will show on the status bar via a notification.

    There's room for improvement, but this code might give you an idea.
     

    Attached Files:

  13. MaxApps

    MaxApps Active Member Licensed User

    YES!!! Thanks.
     
  14. GMan

    GMan Well-Known Member Licensed User

    On my devices (2.1 and 4.2) the App breaks...on the 4.2 device at start, on the 2.1 device after pressing stop (when start pressed)
    Log from the 2.1 device:
    Code:
    Connected: false
    ** 
    Activity (main) Pause, UserClosed = false **
    ** 
    Activity (main) Resume **
    java.lang.NoClassDefFoundError: com.google.android.gms.location.ActivityRecognitionClient
        at anywheresoftware.b4a.objects.ActivityRecognition.Stop(
    ActivityRecognition.java:66)
        at njdude.activityrecognition.sample.main._stopbutton_click(main.java:
    372)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    521)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    174)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    162)
        at anywheresoftware.b4a.BA.raiseEvent(BA.java:
    158)
        at anywheresoftware.b4a.objects.ViewWrapper$
    1.onClick(ViewWrapper.java:66)
        at android.view.View.performClick(
    View.java:2408)
        at android.view.View$PerformClick.run(
    View.java:8816)
        at android.os.Handler.handleCallback(Handler.java:
    587)
        at android.os.Handler.dispatchMessage(Handler.java:
    92)
        at android.os.Looper.loop(Looper.java:
    123)
        at android.app.ActivityThread.main(ActivityThread.java:
    4627)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    521)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    868)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    626)
        at dalvik.system.NativeStart.main(Native Method)
     
    Last edited: Jul 2, 2014
  15. JoanRPM

    JoanRPM Active Member Licensed User

    Yes, it works on my SGII (4.1.2), although it is quite inaccurate.
     
  16. NJDude

    NJDude Expert Licensed User

    It works on most of my devices, but not on the ones running older versions of Android, maybe the library is meant to run only on the latest ones.

    I'm assuming that the inaccuracies you are referring to is due to the way you get the results, like I mentioned on my previous post, it is a little slow but seems to work.
     
    thedesolatesoul likes this.
  17. NJDude

    NJDude Expert Licensed User

    Well, it seems the new Google Play Services update (ver 18) breaks this lib, now I'm getting this error as soon as the app starts.
    Code:
    main_process_globals (java line: 370)

    java.lang.NoClassDefFoundError: anywheresoftware.b4a.objects.ActivityRecognition

        at njdude.activityrecognition.sample.main._process_globals(main.java:
    370)
        at njdude.activityrecognition.sample.main.initializeProcessGlobals(main.java:
    345)
        at njdude.activityrecognition.sample.main.afterFirstLayout(main.java:
    94)
        at njdude.activityrecognition.sample.main.access$
    100(main.java:16)
        at njdude.activityrecognition.sample.main$WaitForLayout.run(main.java:
    76)
        at android.os.Handler.handleCallback(Handler.java:
    733)
        at android.os.Handler.dispatchMessage(Handler.java:
    95)
        at android.os.Looper.loop(Looper.java:
    136)
        at android.app.ActivityThread.main(ActivityThread.java:
    5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    601)
        at dalvik.system.NativeStart.main(Native Method)
     
  18. walterf25

    walterf25 Well-Known Member Licensed User

    Of Course, leave it to google to break stuff!
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    I found the source of this issue. It is fixed for v3.82 which is planned to be released later today.
    Good timing :)
     
    GMan and NJDude like this.
  20. GMan

    GMan Well-Known Member Licensed User

    Hoi Erel,
    also with 3.82 no change - exact the same actions on both devices
     
Thread Status:
Not open for further replies.
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