🐾 Foreground Pedometer by SteveJG

:paw_prints: Foreground Pedometer

=====================

This basic Pedometer app uses the Pedometer component with Ullis' Foreground extension.
The Android Pedometer is used as a Foreground service. The app can either be used as a service (is always active) or as a normal App Inventor app (that goes to 'sleep' and stops working when the user changes apps).

When the Foreground (KAS) service is activated:

  • the app continues to count your steps when and after the device screen closes.
  • users are able to work with other apps while the Service app is active. Even though another app may be active, the Foreground will continue to collect Steps. Steps are posted on the app screen and to a notification.

How to Use the Foreground Steps app

The app Screen provides the controls to use the app in the foreground and collect basic information about your movement while you carry your Android.

The silent notification shows the total number of Steps taken.

  • enable the app

  • select Keep Alive to start the foreground service

  • select Start Counting Steps

  • start walking; the app will count your steps. Check your progress by viewing the Notification.

  • at the end of the day, you might wish to Save Current Steps to DB. Use Retrieve Record of Steps to display. Toggle Record to view or hide your Saved Step Record.

  • Reset Pedometer sets number of Steps to 0

  • Save Prior Steps will start the Step count to wherever you last used the app.

About the Foreground Service

The app uses Ullis' Foreground extension. Ullis' description explains what can be done and what cannot be done using a foreground service and the latest Android version.

The UrsAI2KeepAlive zip archive Ullis Roboter Seite/AI2 Keep Alive contains the source code, the compiled binary for uploading to the App Inventor

The Block code to provide the Service consists in two extensions by Ullis' UrsAI2KeepAlive and UrsAI2WakeLock and the code in the foregroundPedometer aia. provided below.

=====================
Pedometer

How the Pedometer component works Sensors

This component keeps count of steps using the Android device's accelerometer.

Properties

  • Distance returns the approximate distance traveled in meters.
  • ElapsedTime returns the time elapsed in milliseconds since the pedometer has started.
  • SimpleSteps returns the number of simple steps taken since the pedometer has started.
  • StopDetectionTimeout returns the duration of idleness (no steps detected) after which to go into a “stopped” state.
  • StrideLength returns the current estimate of stride length in meters, if calibrated, or returns the default (0.73 m) otherwise.
  • WalkSteps returns the number of walk steps taken since the pedometer has started.

Events

  • SimpleStep(simpleSteps,distance) event is run when a raw step is detected.
  • WalkStep(walkSteps,distance) event is run when a walking step is detected. A walking step is a step that appears to be involved in forward motion.

Methods

  • Reset() resets the step counter, distance measure and time running.
  • Save() saves the pedometer state to the phone. Permits permits accumulation of steps and distance between invocations of an App that uses the pedometer. Different Apps will have their own saved state.
  • Start() starts the pedometer counting
  • Stop() stops the pedometer from counting.

===============================

A simple system to save your steps from counting steps on previous days uses a TinyDB database.

==================================

What to do next

  • add additional code to save and manage your step count or distance walked history beyond the simple system provided.
  • add code to select and save a calibrated step to distance factor instead of the default stride value. Adjust the value to your personal stride allows better distance estimation.
  • add code to save steps to a CloudDB or FirebaseDB to share your Steps with others.
  • add code to email or text the day's steps with someone who cares.
  • add code to a Location plotter so you can show where you have been stepping (create a Track using your GPS). See 🐾 Foreground GPS Location Service experiments and Tutorial by SteveJG for various Tracking foreground apps.
  • walk 10,000 steps a day and see what happens?

=================
Thanks to:

  • Ullis and the ullisroboterseite for the Foreground aix and source.

===================
Perhaps you will find this app useful :astonished:

=====================================
In early September 2013 Google changed how notifications are handled. The StayAlive extension Extension Version: 1.9.0 Date Built: 2022-10-10 no longer works with the aia originally provided in this example. The latest extension (1.10.0
Date Built: 2023-09-22) is now included in the foregroundPedometer2 app aia.

=========================

foregroundPedometer2.aia (62.0 KB)

Regards,
SteveJG

7 Likes

is there blocks screenshot, or aia available?

Read the entire thread (unfortunately it also contains a lot of silent messages that are only visible to power users):

According to my tests neither a WakeLock extension nor the thereby declared permission REQUEST_IGNORE_BATTERY_OPTIMIZATIONS is needed to prevent Doze mode (on Android 6+).

In addition, the permission REQUEST_IGNORE_BATTERY_OPTIMIZATIONS is rejected by Google and the app is not allowed in the Play Store.

Also see here:

Summary

Oh no, the aia was not attached. Sorry. The aia is now at the end of the thread.

1 Like

I find it very good.
With a quick test I saw that it counts the steps correctly,
It would also be useful to be able to delete saved steps. I'll give this a try and see if I can do it with my very little knowledge

A post was split to a new topic: Build a fitness app with Ulis Foreground extension