1

I want to make service running in an interval but service is being killed after some time when app is closed. I know similar questions have been asked multiple times but none of them could answer for what I am looking for. I have tried following approaches

Alarm Manager

By setting the time interval for a pending intent to a broadcast receiver which further lead to run the service. (It keep on working till app is running but it stopped when app is killed.)

  1. It keeps on working in Android 6.0 for long time interval till it is killed by App cleaner manually.
  2. In android 8.0 + it is being killed as app is closed.

JobSchedular

I have also tried JobSchedular to run a JobService but it also killed as the app is closed in Android 8.0+. But it runs for a long time interval in android 6.0 till I run app cleaner and it is killed.

I admit that it is not a good practice to keep service running as it is the loss of device resources. But if someone wants to sync data after some time and generate notifications locally then What to do?

I have also tried cloud messages these are also not received when app is killed. As mentioned here. Various OS behaviors such as Android O's background execution limits may interfere with ability to complete work.

On the other hand

Whats App and skype are generating notifications even if apps are closed and running app cleaner but it is not effecting WhatsApp notifications generator.

What logical strategy these apps adopted that OS is not killing their processes?

I have already tried android:stopWithTask="false" but it does not work. And I do not what to run the service in foreground as well. I have also read. I have also tried to run service again by timer when process is terminated. (Not Worked)

Mashhood Qadeer
  • 482
  • 8
  • 19
  • You must rely in this case on Google cloud messaging. As keeping a service alive by yourself is considered a waste of resources. – Alan Deep May 08 '20 at 19:04
  • @AlanDeep! Question is to handle that issue when app was killed and cloud messages could not received by FirebaseMessagingService. – Mashhood Qadeer May 08 '20 at 19:09
  • check this answer: https://stackoverflow.com/questions/30525784/android-keep-service-running-when-app-is-killed – Alan Deep May 08 '20 at 19:12
  • @AlanDeep! I already asked in my question I do not want service as foreground since it will always have a notification. https://stackoverflow.com/a/52258125/8615158 – Mashhood Qadeer May 08 '20 at 19:20
  • Moreover I have also tried https://stackoverflow.com/a/52708403/8615158 and it does not work for me. – Mashhood Qadeer May 08 '20 at 19:20
  • I don't think you're getting me. https://firebase.google.com/docs/cloud-messaging/ is your bet here – Alan Deep May 08 '20 at 19:22
  • I tried cloud messages but these are not generated when app is killed by OS. Point is what whatsapp doing to generate these notifications without being killed. – Mashhood Qadeer May 08 '20 at 19:25

0 Answers0