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:

rboeck

Well-Known Member
Licensed User
Hi, i have exactly the same error message then K Y Leng; i use B4A 4.3, target SdkVersion 24, Google Play Services 22, on the device Google Play 6.7.76; i tested on a Sony Tablet Z2 and Sony Xperia Z1 compact.

Update: It works with V2.00!
 
Last edited:

jotaele

Member
Licensed User
Its posible that isn't compatible with the latest?

B4X:
main_button1_click (java line: 662)
java.lang.IncompatibleClassChangeError: The method 'void com.google.android.gms.common.api.GoogleApiClient.connect()' was expected to be of type interface but instead was found to be of type virtual (declaration of 'java.lang.reflect.ArtMethod' appears in /system/framework/core-libart.jar)
   at anywheresoftware.b4a.objects.ActivityRecognition.Connect(ActivityRecognition.java:64)
   at com.just4meet.main._button1_click(main.java:662)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
   at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
   at android.view.View.performClick(View.java:4808)
   at android.view.View$PerformClick.run(View.java:19918)
   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:5536)
   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:1397)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1192)

V2.00 has been released. This version is compatible with the recent versions of Google Play Services.
 

hookshy

Well-Known Member
Licensed User
I got this errror do not understand were I am doing wrong ?

C:\android\extras\google\google_play_services\libproject\google-play-services_lib\res\values-v21\appinvite_styles.xml:5: error: Error retrieving parent for item: No resource found that matches the given name '@android:style/Theme.Material.Light.DialogWhenLarge.NoActionBar'.
 

hookshy

Well-Known Member
Licensed User
Use a more recent version of android.jar (under Tools - Configure paths).
changed to android 23 ,the app is downloaded but it crashes
Now, other error is bugging me ...I have android.v4 copied to my lib folders
updated build tools from android sdk manager and I am running google play services v27
I have also copied google play services to my lib folders


Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:b4a.example
** Activity (main) Create, isFirst = true **
java.lang.NoClassDefFoundError: android.support.v4.util.ArrayMap
at com.google.android.gms.common.api.GoogleApiClient$Builder.<init>(Unknown Source)
at anywheresoftware.b4a.objects.ActivityRecognition.Initialize(ActivityRecognition.java:46)
at b4a.example.main._activity_create(main.java:351)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
at b4a.example.main.afterFirstLayout(main.java:100)
at b4a.example.main.access$100(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:78)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5473)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:854)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
 

Erel

Administrator
Staff member
Licensed User
Use 7zip to open android-support-v4.jar (the one that is in the additional libraries folder).

Do you see the ArrayMap file:

SS-2016-03-21_08.47.55.png
 

hookshy

Well-Known Member
Licensed User
Do you see the ArrayMap file:
I have this array.map class in util folder ...the android-suport-v4.jar is copied to aditional libraries
I have deleted suport folder with skd manager and installed again ..then copied again the lib to aditional libraries
 
Last edited:

brunnlechner

Member
Licensed User
Hallo,
i tried the Activity Recognition Example with Activity Recognition V2.1 and google-play-services 29
The app crashes:

Thank You
Franz

LogCat connected to: B4A-Bridge: motorola Moto G 2014
--------- beginning of main
Connected to B4A-Bridge (Wifi)
** Activity (main) Resume **
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:njdude.activityrecognition.sample
Installing file.
PackageAdded: package:njdude.activityrecognition.sample
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (CallSubDelayed - UpdateStatus)
Connected to B4A-Bridge (Wifi)
sending message to waiting queue (CallSubDelayed - UpdateStatus)
Installing file.
PackageAdded: package:njdude.activityrecognition.sample
Installing file.
PackageAdded: package:njdude.activityrecognition.sample
main_process_globals (java line: 530)
java.lang.NoClassDefFoundError: anywheresoftware.b4a.objects.ActivityRecognition
at njdude.activityrecognition.sample.main._process_globals(main.java:530)
at njdude.activityrecognition.sample.main.initializeProcessGlobals(main.java:520)
at njdude.activityrecognition.sample.main.afterFirstLayout(main.java:98)
at njdude.activityrecognition.sample.main.access$000(main.java:17)
at njdude.activityrecognition.sample.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Installing file.
PackageAdded: package:njdude.activityrecognition.sample
** Activity (main) Create, isFirst = true **
main_activity_create (B4A line: 44)
ar.Initialize("ar")
java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/util/ArrayMap;
at com.google.android.gms.common.api.GoogleApiClient$Builder.<init>(Unknown Source)
at anywheresoftware.b4a.objects.ActivityRecognition.Initialize(ActivityRecognition.java:46)
at njdude.activityrecognition.sample.main._activity_create(main.java:355)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at njdude.activityrecognition.sample.main.afterFirstLayout(main.java:102)
at njdude.activityrecognition.sample.main.access$000(main.java:17)
at njdude.activityrecognition.sample.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.util.ArrayMap" on path: DexPathList[[zip file "/data/app/njdude.activityrecognition.sample-2/base.apk"],nativeLibraryDirectories=[/data/app/njdude.activityrecognition.sample-2/lib/arm, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
... 15 more
Suppressed: java.lang.ClassNotFoundException: android.support.v4.util.ArrayMap
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 16 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
Installing file.
Installing file.
Installing file.
PackageAdded: package:njdude.activityrecognition.sample
Installing file.
PackageAdded: package:njdude.activityrecognition.sample
** Activity (main) Create, isFirst = true **
main_activity_create (B4A line: 44)
ar.Initialize("ar")
java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/util/ArrayMap;
at com.google.android.gms.common.api.GoogleApiClient$Builder.<init>(Unknown Source)
at anywheresoftware.b4a.objects.ActivityRecognition.Initialize(ActivityRecognition.java:46)
at njdude.activityrecognition.sample.main._activity_create(main.java:355)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at njdude.activityrecognition.sample.main.afterFirstLayout(main.java:102)
at njdude.activityrecognition.sample.main.access$000(main.java:17)
at njdude.activityrecognition.sample.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.util.ArrayMap" on path: DexPathList[[zip file "/data/app/njdude.activityrecognition.sample-2/base.apk"],nativeLibraryDirectories=[/data/app/njdude.activityrecognition.sample-2/lib/arm, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
... 15 more
Suppressed: java.lang.ClassNotFoundException: android.support.v4.util.ArrayMap
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 16 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
Installing file.
PackageAdded: package:njdude.activityrecognition.sample
** Activity (main) Create, isFirst = true **
main_activity_create (B4A line: 44)
ar.Initialize("ar")
java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/util/ArrayMap;
at com.google.android.gms.common.api.GoogleApiClient$Builder.<init>(Unknown Source)
at anywheresoftware.b4a.objects.ActivityRecognition.Initialize(ActivityRecognition.java:46)
at njdude.activityrecognition.sample.main._activity_create(main.java:355)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at njdude.activityrecognition.sample.main.afterFirstLayout(main.java:102)
at njdude.activityrecognition.sample.main.access$000(main.java:17)
at njdude.activityrecognition.sample.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.util.ArrayMap" on path: DexPathList[[zip file "/data/app/njdude.activityrecognition.sample-1/base.apk"],nativeLibraryDirectories=[/data/app/njdude.activityrecognition.sample-1/lib/arm, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
... 15 more
Suppressed: java.lang.ClassNotFoundException: android.support.v4.util.ArrayMap
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 16 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
running waiting messages (2)
** Activity (main) Resume **
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:njdude.activityrecognition.sample
** Activity (main) Create, isFirst = true **
main_activity_create (B4A line: 44)
ar.Initialize("ar")
java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/util/ArrayMap;
at com.google.android.gms.common.api.GoogleApiClient$Builder.<init>(Unknown Source)
at anywheresoftware.b4a.objects.ActivityRecognition.Initialize(ActivityRecognition.java:46)
at njdude.activityrecognition.sample.main._activity_create(main.java:355)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at njdude.activityrecognition.sample.main.afterFirstLayout(main.java:102)
at njdude.activityrecognition.sample.main.access$000(main.java:17)
at njdude.activityrecognition.sample.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.util.ArrayMap" on path: DexPathList[[zip file "/data/app/njdude.activityrecognition.sample-2/base.apk"],nativeLibraryDirectories=[/data/app/njdude.activityrecognition.sample-2/lib/arm, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
... 15 more
Suppressed: java.lang.ClassNotFoundException: android.support.v4.util.ArrayMap
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 16 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
 

brunnlechner

Member
Licensed User
Thanks, it worked perfectly.
The sample-app apk has by google-play services_lib\res folder almost 3 MB.
Must I use the entire folder google-play services_lib \ res?

Thank You
Franz
 
Status
Not open for further replies.
Top