1

I have implemented Android Speech Recognition as a service on Android 4.1 & 4.2 recognition solution in my application, with some minor modification(e.g: 1500ms countdown, mute beep), which works basically fine.

I would like to run this service for hours. I tested a lot this solution and i experienced unexpected stops. The recognition is running in the background, notification icon is displayed, which indicates, when the recognition service is on. After 10-15 minutes continuous listening without any exception the whole applications stops. (But it happened, after 8 recognition periods as well, after 30 secs.) When it happens, I was able to trace one thing, one more time the thread enters the extended Application class' onCreate() method, where the logs are printed out, but nothing else works. The displayed notification can't be removed.

I don't have leak related exception during listening or any suspicious log message. Is anyone experienced similar stops? Is anyone has any idea why it happens?

I've tried to restart the service after every 20th (handler) starts to avoid this behaviour, so the service is not running for that long. But the situation is the same.

If you have any suggestion what are the possibilities to run the service for many hours, please share.

Community
  • 1
  • 1
  • I've got a similar issue, using cmuSphinx and RecognitionListener. Had you solved this issue in some manner? I tried with wakelock, creating a service and so on, but nothing change. – Hammeronthenet Oct 25 '20 at 16:50
  • It was long time ago, but the accepted answer helped at that time. – AlexIslander Oct 26 '20 at 11:55

1 Answers1

3

BEFORE READING THE FIX BELOW, PLEASE BE AWARE THAT

Keeping a service on for a long time and not allowing the system to kill is a bad idea. The phone has limited battery and the Service runs even when is sleep mode.

Android will kill different services based on the memory state, so on devices with more memory it might not stop. But make it not stop you need to tell the OS that your service is important and, quote from android reference, it can't "be killed without too much harm". To tell the system that you want your service when it can be killed and when it cannot, take a look at startForeground and stopForeground methods, described below.

Start Foreground
It makes the service run in the foreground, making the system prefer to kill other services other than yours to free memory. But, even if you use this method, it is not guaranteed that your service will not stop. It may stop if the phone is on very low memory. Also:

You can set this flag if killing your service would be disruptive to the user, such as if your service is performing background music playback, so the user would notice if their music stopped playing.

Stop foreground

Remove this service from foreground state, allowing it to be killed if more memory is needed.

Links to documentations:
Start Foreground method
Stop Foreground method

Community
  • 1
  • 1
Chaoz
  • 2,119
  • 1
  • 20
  • 39