Android Question Detect "motion" with activityrecognition

Rusty

Well-Known Member
Licensed User
Longtime User
I am trying to implement a service (which is currently running), whereby it doesn't do anything until an "activity"/movement occurs.
In order to conserve battery, I can't just poll the activitymonitor.
Is there a way to create a service or ??? where it "waits" until a motion or activity change occurs and then wakes up the service/activity or???...while conserving battery?
Thanks,
Rusty
 

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Erel, I've tried this and am failing...
The event AR_connected never fires. Any suggestions?

Manifest:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="19"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)

'************ Google Play Services Base ************
AddApplicationText(
<activity android:name="com.google.android.gms.common.api.GoogleApiActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:exported="false"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@Integer/google_play_services_version" />
)
'************ Google Play Services Base (end) ************

SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.
MAIN:
B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
            #AdditionalJar: com.google.android.gms:play-services-location
#End Region

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

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

RecognitionService:
B4X:
#Region  Service Attributes
    #StartAtBoot: False
   
#End Region

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

Sub Service_Destroy

End Sub
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Erel,
I used your code; added logging in the Service Create and Service Start.
I moved to a Samsung S7 (for newer op sys); compiled in release obfuscated; the connection happens, but Service Create nor Service Start ever fire.
I looked through unfiltered and can't see anything that looks like it is my app.
Further ideas?
Rusty

PS I have another app I wrote last fall that puts the AR into the service itself and it does fire the events...
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
All I want to do is detect that any movement has occurred and then ping the activity monitoring for Samsung S-Health so it is not constantly running/polling and running the battery down.
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Erel,
I have it working now. However, I have a question.
When you initialize it with ar.connect(5000), I assume it "polls" the activityrecognition function within the device every 5000 ms.
  1. Does this use much cpu/battery?
  2. Can it be set to only run the recognitionService when something changes? (i.e. still to on foot/walking/...etc.)
  3. Is using this better than running a service (StartServiceAt(5000)) and "polling" then?
Regards,
Rusty
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Erel,
What I'm finding is that if an activity occurs but only for a few seconds, it is not recognized.
i.e. From Still, walk for a few seconds... it doesn't fire
From Still, walk for 15 seconds...it doesn't fire
From Still, walk for another 15 seconds...it does fire.
It seems to include the sum of all intervals...
Do I have a setting wrong or is there a sensitivity setting that says "don't register until X seconds of activity"?
Rusty
 
Upvote 0
Top