1

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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Jam1
  • 629
  • 12
  • 25

2 Answers2

2

Starting from Android 8.0 there are more strong restrictions for background services.To avoid them you should use JobIntentService instead of IntentService. You can read more information about it here.

art
  • 1,222
  • 9
  • 19
  • This is good information, I am reading up on this now, I will update you on the outcome. This seems to be the correct answer using JobIntentService. – Jam1 Dec 26 '19 at 15:51
1

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.

haresh
  • 1,424
  • 2
  • 12
  • 18
  • Yes, they want the job in the form of JobIntentService from what I am getting. – Jam1 Dec 26 '19 at 15:52
  • 1
    Generally speaking, using [`WorkManager`](https://developer.android.com/topic/libraries/architecture/workmanager/basics.html) is even more better approach for background tasks, as it is more powerful and modern mechanism. If you have an extensive amount of background tasks then you should review the ability to use `WorkManager`. This [guide](https://developer.android.com/guide/background/) can be very useful to choose appropriate solution. – art Dec 26 '19 at 16:03
  • This is correct. I am still using the `IntentService`, however I immediately direct it to the `Worker` class I have created so it will do work at the convenient time based on android resources. I am using `OneTimeWorkRequestBuilder` as I am only doing work on direct reply request, so no need for an alarm. – Jam1 Dec 27 '19 at 03:45