B4A Library Physical Activity Recognition Detection

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

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:
Code:
#AdditionalJar: com.google.android.gms:play-services-location
2. Add to manifest editor:
Code:
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
AddPermission(com.google.android.gms.permission.ACTIVITY_RECOGNITION)
AddPermission(android.permission.ACTIVITY_RECOGNITION)
3. Add a receiver named RecognitionReceiver that handles the events.
See the attached example.


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

V4.00 - Based on receiver instead of service.
V3.00 - Released as a class instead of a library. It is based on a new Google API. Not backward compatible.
 

Attachments

Last edited:
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.b4x.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:

Code:
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)
 
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.
 
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.
 
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?
 
Seems to be true, the errors are there but marginal. Maybe I am that fast on foot that the system thinks I am driving ;)
 
For example take i.e. 10 checks and build a middle
 
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.
 
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
 
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.b4x.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:

Code:
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.
Back
Top