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:
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)
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

  • ActivityRecognition.zip
    16.6 KB · Views: 207
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
No - its the actual one
 

brelto85

Active Member
Licensed User
Longtime User
No error. after clicked Connect, the RecognitionService does not start during walk or driving
In the log i've notice only the "Connected: True"
 

brelto85

Active Member
Licensed User
Longtime User
Is not so. there must be something else because even the old method in the project LocationAPI, after updating the SDK, no longer works.

And also, i'm not the only one that have this behavior
 
Last edited:

brelto85

Active Member
Licensed User
Longtime User
I tried keeping running the ActivityRecognition app for about 30 km while driving but don't works

What is your versions of Google Play Service installed on your devices?
i've the 5.0.84 (1259630-034)
 

GMan

Well-Known Member
Licensed User
Longtime User
I have the same version
 

brelto85

Active Member
Licensed User
Longtime User
I've found the cause!!!!!!!!!!!!
Is necessary enable the "Google's Service Location" in position settings on device
 

GMan

Well-Known Member
Licensed User
Longtime User
Nope - i had this setting already. Still the same: crashed at Startup
 

brelto85

Active Member
Licensed User
Longtime User
1. With this library is possible to allow to start up my app if i detect if in_vehicle without the Connect() in the activity?
2. The name of service must be "RecognitionService" or can be also "ServiceRecognition" if in the manifest file i write "ServiceRecognition"?
3. The activity is necessary? can i managed the recognition with just the service?
 

GMan

Well-Known Member
Licensed User
Longtime User
LoL - got it working.
I found the google...jar in the AdditionalLib folder - moved it to the internal one and: thats it
 

brelto85

Active Member
Licensed User
Longtime User
1. With this library is possible to allow to start up my app if i detect if in_vehicle without the Connect() in the activity?
2. The name of service must be "RecognitionService" or can be also "ServiceRecognition" if in the manifest file i write "ServiceRecognition"?
3. The activity is necessary? can i managed the recognition with just the service?

someone can help me? thanks
 

NJDude

Expert
Licensed User
Longtime User
1. With this library is possible to allow to start up my app if i detect if in_vehicle without the Connect() in the activity?
2. The name of service must be "RecognitionService" or can be also "ServiceRecognition" if in the manifest file i write "ServiceRecognition"?
3. The activity is necessary? can i managed the recognition with just the service?
1- Yes, but you have to connect, that starts the service.
2- You must use the name as indicated on post #1.
3- You need to start the service, so you will need an activity.
 

brelto85

Active Member
Licensed User
Longtime User
I have not explained properly

I have an option in the settings of the app that allows me to enable auto-launch the application when it is in the car
I've the following problems:

1) If i call a CallSub(Main, "..") to connect the activityrecognition object into main activity, doesn't works properly and the Main sub do not called
2) If i call a CallSubDelayed(Main, "..."), it open the Main activity but this behaviour it's not correct for the app's operativity
2) If i try to force the Connect() into Activity_Resume, the connect event works, but the RecognitionService do not start

Simply needs to activate the background service when it is in the car
the ActivityRecognition sample project works properly
 

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am trying to test this but when I compile the app it comes up saying:

B4X:
Parsing code.  0.02
Compiling code.  0.05
Compiling layouts code.  0.03
Generating R file.  Error
ERROR: resource directory '<SDK_PATH>\extras\google\google_play_services\libproject\google-play-services_lib\res' does not exist

I then checked and made sure that Google Play Services was downloaded and it was:

sdk_screenshot.PNG


I then changed the #AdditionalRes to:

sdk_location.PNG


Since this is where the SDK is kept but now it comes up with the following error:

B4X:
Parsing code.  0.03
Compiling code.  0.07
Compiling layouts code.  0.02
Generating R file.  0.87
Compiling generated Java code.  1.08
Convert byte code - optimized dex.  Error
A referenced library is missing: google-play-services

Any ideas on what I have done wrong?
 
Status
Not open for further replies.
Top