Search code examples
androidandroid-notificationsintentserviceandroid-intentservice

Intent Service works but class it calls to do work stops when app is closed


I have an IntentService called ReplyMessageIntentService in my app, and this service is responsible for receiving messages that comes from notification direct reply. The intent service does immediately calls a class SaveMessage that saves messages to a database. Other classes and activities in my app uses the SaveMessage class as well.

ReplyMessageIntentService and SaveMessage works well together. However, when the app is closed for a long period time, if I should reply to a notification, ReplyMessageIntentService is called as it should, works fine; SaveMessage is also called/initialized, but stops before even saving anything to the database. And the notification spinner in the notification tray continues to spin as the code stops before I can send feed back to cancel/update the notification.

I suspect I am using IntentService the wrong way, however, I do not want to write more code for saving data to the database inside the intent service as I already have code for that. If the previous statement is not the problem, I assume that IntentService has a certain duration of time that a task must be completed when app is closed for a period of time. Please let me know the best solution for this problem.


Solution

  • It would be more good if you use workmanagar it will call alarm managar till API 14 and job scheduler after Api level 14 in background also you can set different constraint.