2

I wrote an android app a few years ago that does a fairly simple process. I set up a condition series consisting of different paces (walk, jog, sprint) with times associated with each segment: Walk 1 min, job 2 mins, sprint 3 mins. When I press the go button the app will display a countdown timer for each segment, a complete time timer and the pace (using GPS). There is a display, but the main feature is that the app uses sound to tell me long long till the next transition and what is coming up next.

For example, when it starts it produces a sound every second for 5 seconds then says "2 minutes". At the 30 sec mark a sound will occur. At 1 minute it will say "1 minute", another sound at 30 sec, then at 10 seconds before changing pace it will say "Next leg (jog) (or walk or sprint)" and play a sound the last 5 seconds. This way I do not need to look at the display to know where I am at in the condition program I created.

the problem: When I created this and ran it I was running two versions of Android behind the current one I now have (8.1.0) and now I run it on an LG Stylo 4. back then the device would go into sleep/standby mode, the screen going dark automatically or I hit the power button and the app would continue to operate just fine. Time would tick down, sounds would be made right on time.

This is the first time I installed and ran my app on the new phone/OS and let's say it did not go well. At one point I though I had 30 secs left and I didn't hear anything for over a minute. It seems like the app is now half getting paused, like 30 seconds turns into over 1 minute and there is no sound on the transitions.

Something has changed with Android it seems for this did not happen in past releases. What I want is for the program to keep running in real time when the device switches to sleep mode and while I read about excluding the app from Battery savings, that did not work.

In what way can I keep this app running in the background, counting down AND making sounds even when the phone is in sleep mode.

  • OS version is 8.1.0
  • Kernel 3.18.71
  • build OPM1.171019.019
  • Device: LM-Q710TS (LG Stylo 4)

The specific code in question is this:

    //runs without a timer by reposting this handler at the end of the runnable
Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable() {

    @Override
    public void run() {

        // do something here to display

        processTime();    // process what to be done on a sec by sec basis
        try {
            timerHandler.postDelayed(this, 1000);
        } catch (Exception ex){

        }

    }
};

I saw post that referred to using partial wakelock or maybe another version of timer, but can't find it now. Hoping there is a straight forward answer to this conundrum.

j.hull
  • 321
  • 3
  • 15
  • I forgot to mention that in the past I would run edomondo at the same time (at times) so maybe that had something to do with it, but many times I just ran the app on its own. – j.hull Jan 27 '19 at 23:20
  • I'm not quite sure if that is the root of your problem, but maybe [this](https://developer.android.com/about/versions/oreo/background) has something to do with it. Maybe running the code in question in a [foreground service](https://developer.android.com/guide/components/services#Foreground) is the solution. – greyhairredbear Jan 31 '19 at 20:18
  • Thanks. The first link does not directly talk about actions like a timer, but it did touch on sound and I use sound as part of the timer. Setting foreground service may work, but dumb question, what constitutes a service. I have an App (as I understand android) not a service. Can I somehow set the app to act like a service? The code snippet is not clear, but would this go in my OnCreate function on where I start the timer? – j.hull Feb 04 '19 at 16:33
  • As far as I'm concerned, there are no dumb questions :) To create a foreground service, you would need to extend the Service class and call `startForegroundService()` a `Context` object (may be your activity). Then, in your service class, you can call `startForeground`. See e.g. [this](https://stackoverflow.com/questions/6397754/android-implementing-startforeground-for-a-service) question, in the top answer you can find a link to an example project, which I will also add here for the sake of completeness ([see here](https://github.com/commonsguy/cw-omnibus/tree/v8.4/Notifications/Foreground)) – greyhairredbear Feb 05 '19 at 09:38
  • I looked into the two links and while I get the idea of a service (I liken it to a Windows Service process), my app (my first medium sized Android App) was not set up/designed to run as a service. My Main Activity is the primary UI form and other UI activities lead from there. Here is a link to my github source. https://github.com/bucc5062/TrackItEq_1.0 Long time .net programmer, kind of new to android and Java. The activity in question is TrackItEqDisplayActivity.Java. This is my home brewed app for conditioning with horses. – j.hull Feb 05 '19 at 16:05
  • If what i did continues to work I will mention it in Answer, but I'm not convinced. The writing to Log seemed to keep the app working in sleep mode. I also told the device to exclude the app from sleep, but that didn't seem to work at first. At the moment, however, it counts down even if I tap the power button. – j.hull Feb 05 '19 at 16:08
  • 1
    I don't know what you mean with "writing to Log", but normally a foreground service should work here, and from quickly reading over your code I think it should be possible to refactor the code in question to a service. Audio players that continue to play songs are for example using foreground services, therefore I think it should be applicable to your needs. – greyhairredbear Feb 06 '19 at 19:05
  • to LpeteR90, you were correct that my idea of "Writing to a log" did nothing. For a moment I thought it was working again fine, but after another test, no joy. I'll have to dig into how to change parts of this to a service. Java android is not a comfortable environment for me (sigh), but I think I need to move the timer part out of the UI(?) while still allowing it to communicate when the UI is displayed. Maybe a couple hints on class structure or flow(?). Like, when the user starts a session the UI creates/calls a service that is set to run in foreground? I'll get this yet. – j.hull Feb 15 '19 at 15:19

0 Answers0