0

I am trying to create a fitness app that will monitor the steps that i take. Ive been testing for almost 7 days now, and im really struggling to achieve this.

So far i managed to create code that run when the screen is on, but it will stop running after the screen is turned off after 1-3 minutes. My code simply saves a word every 1 min, or 10 secs or 1 sec, i tested with all 3 configurations.

I tried using the following:

  • AlartManager (all of the difference alarms)
  • WakefulBroadcastReceiver
  • Broadcastreceiver
  • Service (StartForeground)
  • Wake Locks
  • To add my app to the whitelist so that "Doze mode" on the Samsung device wont intefear.
  • To totally remove the app from the battery optimization

I got over 10 different examples on my PC, all of them preform as described. So i will try a different approach and ask a simple straight out question.

Testing devices

HTC M8 one Android v6.0.1

Samsung Galaxy s6 edge Android v6.0.1

Question

All i want is code that can run when the screen is black, also after 30 mins and more. Not just 1-3 minutes. It seems to be more challenging than balancing on 1 finger :-)

Can anyone help me with some code so that i can go in the right directions?

Sorry for not posting real code in this question, i felt it was not appropriate since i tried so many things, that this post would be 67 scrolls long :-)

Prior questions

If you want to see some off the code i created before in order to acvieve what i want here is a post. Android OS shuts down the Wakelock + AlarmManager after a few minutes

Community
  • 1
  • 1
Daniel
  • 2,002
  • 5
  • 20
  • 32
  • Can you define what you mean by 'steps'? A calculated value based on user height and distance moved or actually counting the changes in the accelerometer? – Morrison Chang Oct 03 '16 at 16:24
  • Yes sure. Every step you take is a step. Nothing more to it. There is a sensor in the Android lib. which can count the steps taken already. Anyways, it is not that important how the steps is calculated. – Daniel Oct 03 '16 at 16:27
  • I noticed that you mentioned in your prior question that you tried: http://stackoverflow.com/questions/35666127/difference-between-setandallowwhileidle-and-setexactandallowwhileidle - can you describe your testing process for both HTC and Samsung. – Morrison Chang Oct 03 '16 at 16:45
  • @MorrisonChang if you click the link that i provided in my question you can read all about that. Nothing to much to it really, the app just stops running after a few minutes. – Daniel Oct 03 '16 at 16:48
  • yes it stops, but does your app startup again in an hour or two (doze mode - 10 minutes is too short). I would try your app with inexact and a 1 hour setting first to see if it fires consistently. – Morrison Chang Oct 03 '16 at 17:04
  • Are you able to join the chat that i created? – Daniel Oct 03 '16 at 17:05

2 Answers2

0

You can do this sort of thing with an Executor like this:

    scheduler = Executors.newSingleThreadScheduledExecutor();

    scheduler.scheduleAtFixedRate
            (new Runnable() {
                public void run() {
                   // your stuff goes here
                }
            }, 0, 10, TimeUnit.SECONDS);
J.B
  • 123
  • 1
  • 7
  • I don't think you need any more code, apart from the stuff you want to run - that code will run whatever you call in the middle bit ( // your stuff here). As written tt will run every 10 seconds, but you can change the parameter or units. – J.B Oct 04 '16 at 14:53
0

I have something similar, my approach is to handle this on a service or service intent. The service itself schedule an AlarmManager for the next time it has to execute so whenever the service is started the intent is handled and at the end a alarm is set only for the next time it has to do its thing again.

The gotcha is that Android does not like this approach too much if it is a repeating alarm or if the schedules are too short between each other. It works OK if you have something that runs every 15min or so.