B4A Library Physical Activity Recognition Detection

Status
Not open for further replies.
Class updated on May 2020.

This class 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:
B4X:
#AdditionalJar: com.google.android.gms:play-services-location
2. Add to manifest editor:
B4X:
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
AddPermission(com.google.android.gms.permission.ACTIVITY_RECOGNITION)
AddPermission(android.permission.ACTIVITY_RECOGNITION)

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:
B4X:
Sub Process_Globals

End Sub
Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)
    For Each ev As TransitionEvent In Starter.client.GetTransitionEvents(StartingIntent)
        Log(ev) 'ignore
        ToastMessageShow("" & ev, True)
    Next
    Service.StopAutomaticForeground
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.


Listening for events

B4X:
'from an activity
Dim p As Phone
If p.SdkVersion >= 29 Then
    rp.CheckAndRequest("android.permission.ACTIVITY_RECOGNITION")
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        ToastMessageShow("No permission", True)
        Return
    End If
End If
Dim activities As List = Array("IN_VEHICLE", "ON_BICYCLE", "ON_FOOT", "STILL", "WALKING", "RUNNING")
Wait For (Starter.client.RequestTransitionUpdates(activities)) Complete (Success As Boolean)
Log(Success)


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

V3.00 - Completely new version, released as a class instead of a library. It is based on a new Google API. Not backward compatible.
 

Attachments

  • ActivityRecognition.zip
    9.4 KB · Views: 278
Last edited:

aaronk

Well-Known Member
Licensed User
You also need to copy google-play-services.jar to the additional libraries folder.
That seems to fix the problem.

However now I have another problem when I run the app.
When I run the app it comes up saying: "Unfortunately, Activity Recognition Example has stopped."
then when I press OK the app just closes.

This happens in both Debug(Legacy) and Release Mode.

I am using this code: http://www.basic4ppc.com/android/forum/attachments/activityrecognition_sample-zip.26265/

Running this on my Sony Xperia Z (Android 4.4) and also tried it on my Nexus 5 (Android L) and both get the same error.

When I run it in Debug(Legacy) and with the filter un-ticked the IDE logs:

B4X:
LogCat connected to: B4A-Bridge: Sony C6603-355762054175613
--------- beginning of /dev/log/main
PackageAdded: package:njdude.activityrecognition.sample
Installing file.
PackageAdded: package:njdude.activityrecognition.sample
main_process_globals (java line: 486)
java.lang.NoClassDefFoundError: anywheresoftware.b4a.objects.ActivityRecognition
 at njdude.activityrecognition.sample.main._process_globals(main.java:486)
 at njdude.activityrecognition.sample.main.initializeProcessGlobals(main.java:461)
 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:212)
 at android.app.ActivityThread.main(ActivityThread.java:5151)
 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:878)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 at dalvik.system.NativeStart.main(Native Method)
 

aaronk

Well-Known Member
Licensed User
Don't ask me why, but my computer was running 3.80 even knowing I updated to 3.82 the other day.

Now since I have updated to 3.82 it is running fine.
 

grafsoft

Well-Known Member
Licensed User
Njdude: I only have version 3.80. Should have read the postings, sorry. I do not find a link to the newest version. Can you help me? Perhaps an email to office at grafsoft.at.
 

NJDude

Expert
Licensed User
Njdude: I only have version 3.80. Should have read the postings, sorry. I do not find a link to the newest version. Can you help me? Perhaps an email to office at grafsoft.at.
If you are referring to a link to B4A 3.82, then you should contact support@basic4ppc.com.
 

grafsoft

Well-Known Member
Licensed User
I just did. But what puzzles me - I get plausible values for still, unknown and tilting, but much too much for in_vehicle. I never was in a vehicle for the last few hours. I know this is probably not your fault, but do you have any idea?
 

NJDude

Expert
Licensed User
I tried that code on foot, car and bike, the results are somewhat accurate, that's all I can tell you.
 

grafsoft

Well-Known Member
Licensed User
Seems to be true, the errors are there but marginal. Maybe I am that fast on foot that the system thinks I am driving ;)
 

GMan

Well-Known Member
Licensed User
For example take i.e. 10 checks and build a middle
 

RAJAN MBA

Member
Licensed User
Maybe I am that fast on foot that the system thinks I am driving ;)
What confidence level did you get ? I think anything below the 60% range is suspect. I found Windows phone 8.1 Sensor core to be better than Android OS in detecting activities.
 

brunnlechner

Member
Licensed User
The ActivityRecognition_Sample.zip does not work with Android5.

ar.Connect(5000) 'Set the detection interval to 5 seconds.

ERROR:
** Activity (main) Resume **
java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.google.android.location.internal.GoogleLocationManagerService.START }
at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1773)
at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1872)
at android.app.ContextImpl.bindService(ContextImpl.java:1850)
at android.content.ContextWrapper.bindService(ContextWrapper.java:538)
at com.google.android.gms.internal.v.a(Unknown Source)
at com.google.android.gms.internal.u.connect(Unknown Source)
at com.google.android.gms.location.ActivityRecognitionClient.connect(Unknown Source)
at anywheresoftware.b4a.objects.ActivityRecognition.Connect(ActivityRecognition.java:56)
at njdude.activityrecognition.sample.main._startbutton_click(main.java:517)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:163)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:159)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:66)
at android.view.View.performClick(View.java:4761)
at android.view.View$PerformClick.run(View.java:19759)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

Thank You
 

NJDude

Expert
Licensed User
The latest and greatest on both, it doesn't work, however, if you try the APK it does work, Google has changed something.
 

KY Leng

Member
Licensed User
That seems to fix the problem.

However now I have another problem when I run the app.
When I run the app it comes up saying: "Unfortunately, Activity Recognition Example has stopped."
then when I press OK the app just closes.

This happens in both Debug(Legacy) and Release Mode.

I am using this code: http://www.basic4ppc.com/android/forum/attachments/activityrecognition_sample-zip.26265/

Running this on my Sony Xperia Z (Android 4.4) and also tried it on my Nexus 5 (Android L) and both get the same error.

When I run it in Debug(Legacy) and with the filter un-ticked the IDE logs:

B4X:
LogCat connected to: B4A-Bridge: Sony C6603-355762054175613
--------- beginning of /dev/log/main
PackageAdded: package:njdude.activityrecognition.sample
Installing file.
PackageAdded: package:njdude.activityrecognition.sample
main_process_globals (java line: 486)
java.lang.NoClassDefFoundError: anywheresoftware.b4a.objects.ActivityRecognition
at njdude.activityrecognition.sample.main._process_globals(main.java:486)
at njdude.activityrecognition.sample.main.initializeProcessGlobals(main.java:461)
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:212)
at android.app.ActivityThread.main(ActivityThread.java:5151)
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:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
at dalvik.system.NativeStart.main(Native Method)

I have similar problem like Aaronk, even I use B4A version 4 and Google Play services revision 22 and change the additional res to s.th like this in my PC.
#AdditionalRes: C:\Users\DaunPenh\AppData\Local\Android\sdk\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms

When I complile, it is ok but when the app is loaded, it just popup the message "Unfortunately, Activity Recognition Example has stopped"
and in the log I got something like below:

** Activity (main) Create, isFirst = true **
java.lang.NoClassDefFoundError: com.google.android.gms.location.ActivityRecognitionClient

at anywheresoftware.b4a.objects.ActivityRecognition.Initialize(ActivityRecognition.java:45)
at njdude.activityrecognition.sample.main._activity_create(main.java:304)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at njdude.activityrecognition.sample.main.afterFirstLayout(main.java:98)
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:5118)
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:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at dalvik.system.NativeStart.main(Native Method)


Help me please !!!
 
Status
Not open for further replies.
Top