11

This question not duplicate of onTaskRemoved() not getting called in HUAWEI and XIOMI devices

Problem:

When I press home button & kill the app onTaskRemoved() (Service class override method) - not called.

If I press back button & kill the app --> onTaskRemoved() called perfectly

This issue happen in Android lollipop versions & oreo versions

MyService.class -> Manifest declaration

    <service android:name=".MyService"
        android:label="MyService"
        android:stopWithTask="false"
        android:enabled="true"
        android:exported="true"
        />

I already used the return START_STICKY; in onStartCommand()

Tested devices

Lenovo, Samsung - lollipop version

Samsung - oreo version

Any suggestions or comments are welcome. Your small tips will help to fix this huge issue.

Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
  • I have been gone through similar issue didn't get the exact solution for it .What i manage to understand [whitelisting](https://developer.android.com/training/monitoring-device-state/doze-standby.html#whitelisting-cases) of app is the culprit or maybe i got it all wrong. Tested only on xiaomi and it worked somehow after enabling the AutoStart . Seems like we need an expert's advise on that one . – ADM Mar 26 '18 at 08:16
  • @ADM thanks for such valuable points. – Ranjithkumar Mar 26 '18 at 08:19
  • 1
    On android O its quite clear, You cannot run background service for longer time if your app goes in background. Based on the [documentation] (https://developer.android.com/about/versions/oreo/background.html#services) , OS will terminate the service as if you have called stopSelf. But its strange behavior on Lollipop. Is onDestroy() called on Lollipop? – Sagar Mar 29 '18 at 00:55
  • @Sagar Then how whatsapp notification working in Oreo? Yes strange issue in some lollipop devices also. – Ranjithkumar Mar 29 '18 at 05:02
  • Notification and Services are two different things. Whatsapp might be receiving high priority push notification and displaying. As said by @ADM you can whitelist your app to avoid these restrictions. Since whatsapp is VOIP and messager app they can easily do it. In your case, can start a foregroundService to keep the Service alive until your job is finished. Android O provides API this. – Sagar Mar 29 '18 at 06:23
  • Agree with Android O . But what i have figured out on some custom OS devices running on L and M(Oppo, Xiaomi) FCM high priority message did not received in some scenario(Reboot) . So thats make it even worse for me, Service is not running FCM not received and i am working on WebRTC calling app in which i have to maintain session in background.I am kind of stuck here too. In IOS we are using VOIP push and it was working fine. Is there anything equivalent to VOIP in android ? – ADM Mar 29 '18 at 06:28
  • @ADM Same problem here. I am also use the WebRTC that's the reason for this question. Hope you get answer within one week by any expert :) – Ranjithkumar Mar 29 '18 at 12:28
  • @RanjithKumar what does your onTaskRemoved() method does? May be we can propose other alternatives to achieve your target. – Sagar Mar 30 '18 at 11:12
  • @Sagar our only target is 24 hours running service for push WebRTC messages. If service killed --> we need to restart service. onTaskRemoved() --> this method only we can restart service, I am right? – Ranjithkumar Mar 30 '18 at 11:22
  • Based on your requirement, you can use the [Whitelist features](https://developer.android.com/training/monitoring-device-state/doze-standby.html#support_for_other_use_cases) provided by Android. This will ensure your backgournd is running on android O. You can check the criteria [here](https://developer.android.com/training/monitoring-device-state/doze-standby.html#whitelisting-cases) – Sagar Mar 30 '18 at 13:52
  • You can check if onDestroy() is getting called on Lollipop. If so then the work around is either attempt to restart the service or schedule an Alarm Manager to trigger the service start after few seconds. – Sagar Mar 30 '18 at 13:54
  • @Sagar If possible can you post your comment as detailed answer. Bounty available for next 5 days. So you can post your answer as per your convenience – Ranjithkumar Mar 30 '18 at 14:26
  • How exactly do you _"kill the app"_? – Onik Mar 31 '18 at 01:20
  • 1
    @Onik press the home button & force remove from recent apps – Ranjithkumar Mar 31 '18 at 13:07

1 Answers1

4

Based on your use case, you should be able to meet the criteria for the White listing on Android N and above. You can follow this link to Whitelist your app. By requesting this permission, you can relax some restrictions (like accessing network or holding partial lock) implied by Doze mode and Android O. These restrictions are imposed by OS on the app those are not white-listed.

For Lollipop: Certain manufacturers using cyanogenmod or other custom implementation, could have impact on the intended behavior of START_STICKY. Workaround in this case would be to rely on onDestroy() method of service to:

  1. Restart the service.
  2. Trigger an AlarmManager which will trigger after few seconds and start the service.

If you use approach 2:

On normal devices where the START_STICKY behaves as intended, you can use the AlarmManager to check if service is running by:

  1. Maintain a static variable in service to check if service has been started
  2. Cancel the AlarmManager onStartCommand() of the service.
Sagar
  • 23,903
  • 4
  • 62
  • 62
  • Thanks Sagar. Can you give few more details about whitelisting. I am confusing & still not understand. – Ranjithkumar Apr 05 '18 at 04:58
  • @RanjithKumar You can consider whitelistling as a way to partially relax the restrictions imposed by Doze and App Standby mode. Your app can access network and hold partial locks during doze mode with out restriction, which otherwise are restricted for apps that are not in whitelist. Only thing to be careful is to ensure that you app matches the criteria for white listing apps. Does that help you so clear the confusion? If not we can open a chat session and I can help you to understand better. – Sagar Apr 05 '18 at 05:43
  • I will read document completely & I ask you again. Thanks for your support :) – Ranjithkumar Apr 05 '18 at 06:01
  • @RanjithKumar Can you find a solution regarding this issue? In a help. Let me explain my case, I have to get a token from web service for background upload, but my token validity is one hour, so again make a call after an hour. This scenario leads to crash my app while it is in the background(State not OPENED). Thank you in advance. – MohanRaj S Dec 28 '18 at 06:31
  • 1
    @MohanRajS pls try Evernote job service & autostart feature. if we use both these things we can keep alive service in 90% devices. not 100% – Ranjithkumar Dec 29 '18 at 11:29
  • @RanjithKumar Thankyou for your support. – MohanRaj S Dec 31 '18 at 07:44