[FREE] Pedometer with Foreground Service

Excuse my english I use google translator
This extension is a continuation of my first [FREE] Pedometer Sensor (not accelerometer) extension, it is a full-featured pedometer for creating a fitness app, which includes a foreground service with its corresponding notification.
First of all, we are going to define what a service is in android, especially since sdk 8, the service allows the application to continue counting steps despite being closed, but android one of its main tasks is to avoid excessive battery consumption so it kills services when memory usage is compromised.
To avoid this we must create a service that consumes the minimum of memory and that the bulk of the calculations is done by the activity, therefore the service only performs the following functions:

  • Count steps
  • It counts the elapsed time (the real one, that is, it only counts the time when we are walking, not in a continuous way)
  • Creates a daily database with the steps taken and the time if the user uses ResetDaily.
  • Respond to the requests of the activity to reset the steps and time.
    The rest is done by the activity.

In addition, the user must allow the AutoInit of the service and the use of the battery without restrictions (the battery consumption by the service is minimal)
for this there are these two blocks:

This does not lead to the AutoInit configuration
bootinit
and this will take us to a page where it explains depending on the brand of the device how to use the battery without restrictions
urlhelp
If all these concepts are configured correctly the service will count steps indefinitely if you do not stop the service
Properties
start stop reset isactive
The pedometer
The extension allows the use of two types of sensors to count the steps: the accelerometer or the Sensor Detector of steps that some devices carry.
for this we can use these blocks
sensortipe setsensor issensor
Another feature of the extension is that it uses a WAKE_LOCK to prevent the activity from entering Doze mode when it has not been closed, this would allow us if the application is not closed for example, that the app warns us every time we perform 1000 steps, although is asleep, to avoid closing the application the extension has the following block that mimics the Home button of the device.
homekey
As we already mentioned, the service calculates the steps and the time and the activity is in charge of calculating the distance (km or miles), the calories consumed, the speed (Km / h or Miles / h).
for this we use these blocks
eventWalkStep calories distance formattime getsteps gettimes kmtomille speeed
For this, the user must supply the following data: stride length, weight and daily goal.
for this we use these blocks
setgoal setlen setpeso
To handle the database we will use these blocks
getdata getcalories getdistance getstep gettime
All data (database, step length, weight, goal ...) are permanently saved in the app when we change it, being available at the next start of the app

Two other features of the application is that it allows you to create a daily base of the steps through ResetDaily
setdaily
And whether or not to allow the service notification to open the activity when we click on the aforementioned notification

I would like to thank Ullis Robert Seite Ullis Roboter Seite/AI2 Keep Alive
for its source of service that served as the basis for mine.

es.mariosoft.AcePedometer.aix (49.8 KB)
AceSteps.aia (57.8 KB)

For the latest version see post 14 here

11 Likes

the app will not work in the companion for it to work you must compile it

2 Likes

Version 2 is attached that solves a Bug for which the time did not reset properly when the ResetDaily is active

es.mariosoft.AcePedometer.aix (50.0 KB) (v2)

3 Likes

Hi, I just need to test your AcePedometer as you posted with aia file. But it shows each time Runtime error.
"Neither user 10394 nor current process has android.permission.WAKE_LOCK.
Note: You will not see another error reported for 5 seconds."

Please tell me how I can solve this problem.
Thanks.

Hello, thanks for app.
Time in notification is 2 hours longer:

Why isn't there a feature to turn the foreground service off and on? I think it would be great to add. Thanks.

this extension unfortunately does not work anymore after the SDK31 release...
@Mario1 please see the error message and specify FLAG_IMMUTABLE when creating a PendingIntent...
Unbenannt2

Taifun

Modification of the extension prepared for sdk31

Versión 3

es.mariosoft.AcePedometer.aix (50.0 KB)

AceSteps.aia (57.0 KB)

Un saludo y buena suerte

3 Likes

Thanks Mario. However I get on start

on my Android 12 using your version 3 aia example. Any ideas?

Edit: this happens in Companion (read your post #2, thanks... seems to work using apk. :slight_smile: but time is not correct..will work on that.

--Steve

Yes, I can confirm that
This already has been reported here

For me the difference is 17 hours

Taifun

I think the time issue is fixed, it seems to work fine now

ima

I upload the extension again and aia

es.mariosoft.AcePedometer.aix (49.8 KB)

AceSteps.aia (56.8 KB)

Un saludo

1 Like

I installed the latest version, but I still have an issue with the time... it starts with 17:00:00, see screenshot... I'm located in Costa Rica, which is GMT-6...

EDIT: some more Screenshots, a new day


Starting from 8am it looks more reasonable

Taifun

New version 4

Much of the code is rewritten.

First of all to adapt the notifications to the new versions of Android

The way of formatting the time that gave errors in previous versions has been modified

The management of the database is no longer carried out by the service and is carried out from the application itself in the aia file, there is an example of how to do it.

Due to this the following blocks disappear:

image image image
image image
image

The following blocks also disappear because they are no longer needed

image image

The extension no longer uses WAKE_LOCK, to manage Doze mode, and needs Battery usage to be "Unrestricted" so that no steps are missed.

At the beginning of the App, a dialog box will open for you to manage this theme and will take you to its corresponding configuration

New blocks have been added to facilitate making a sports application

With these two blocks, it allows the application to notify us in a spoken way every X steps of the steps and time we have taken, as well as of each kilometer or mile traveled

And with these blocks it allows us to send a toast message and know what the language of our phone is

blocks (1)

blocks (1)

I hope you like it and that you serve as a beta tester

Un saludo y Buena Suerte

es.mariosoft.AcePedometer.aix (52.5 KB)

AceSteps.aia (62.1 KB)

2 Likes

Thanks for your efforts Mario.

Version 4 AceSteps aia is not working. Here is screen shot of the apk loaded on a Samsung A13 Android 12.

The pedometer does not count (does not increment steps); Time does not increment. It is just not working. :cry: The problem just might be the A13. Previous versions 'worked' with missed steps and time issues.

--Steve

Why would (disabling) battery optimization be needed when using a Foreground service?

Because if you don't do it at a certain time android sleeps the service and stops counting steps, if you put it "No Restrictions" android never sleeps the service, battery consumption is insignificant

You have configured the battery optimization to "No Restrictions", if you do not do it, the service will not start.

If you use the "Sensor Detector", check that you have authorized the "ACTIVITY_RECOGNITION" permission

But disabling battery optimization is not allowed in the Play Store.
You can try looping and playing a silent (short) sound with the Player component to prevent Doze mode using a Foreground service.


See here.

1 Like

The extension itself does not disable battery optimization, that has to be done by the user of the app, the extension only indicates where to do it.

All devices behave unfortunately, the stock android devices run fine, but some devices does not, they stop any kind of foreground service within 12 - 11 hours even if high priority is set.