I want to develop Text-To-Speech (TTS) feature in my app. It must be implemented inside Service, because user may leave "details" Activity and get back to "main" Activity or even leave my app, and it should still speak out loud text. Since Oreo introduced some background limitations for services and I must support 4.0+ I have to use JobIntentService
Problem is that TTS have async implementation and JobIntentService gets killed just after onHandleWork finishes its job, even when I use startForeground (in my code showSpeakingNotification)
Funny part is that when I but a breakpoint inside onHandleWork after 'speakOut' method or just uncomment Thread.sleep service is working and reading my text (and foreground notification is present).
Question is how to prevent "auto-killing" my service when it is actually running, but using asynchronous feature inside?
@Override
protected void onHandleWork(@NonNull Intent intent) {
if (ACTION_SPEAK.equals(intent.getAction()) && intent.hasExtra(EXTRA_TEXT_ARRAY)) {
ArrayList<String> textArr = intent.getStringArrayListExtra(EXTRA_TEXT_ARRAY);
showSpeakingNotification(textArr.get(0));
if (ttsInitialized)
speakOut(textArr);
else if (ttsInitListener != null)
ttsInitListener.setPendingText(textArr);
} else if (ACTION_STOP_SPEAKING.equals(intent.getAction())) {
if (ttsInitialized && tts.isSpeaking())
tts.stop();
}
try {
//Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}