With the introduction of Doze in Android M and Android getting more and more restrictive with background executions during Doze. Could someone elaborate if the same thing applies when extending Android's own services. For example NotificationListenerService?
I am using it as such
public class ListenForNotificationsService extends
NotificationListenerService {
private String TAG = this.getClass().getSimpleName();
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.d(TAG, "Notification received");
// Do AsyncTask() for network request
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
if (sbn.getPackageName() != null) {
Log.d(TAG, "Notification removed:" + sbn.getPackageName() + ":" + sbn.getId());
}
}
}
It stops working after a couple of minutes when the device goes into Doze. I have tried using AlarmManager, JobScheduler, JobIntentService and normal IntentService. None of them work when the device deep sleeps.
As a side note, my device is a Huawei Mate 10 Pro. And they have been known for aggressive task management. But I have Whitelisted my app, disabled everything that can be disabled from Huawei's side that kills off apps in the background.
Any advice on how I can call an AsyncTask while the device is Dozing (relative to Google's rules about maintenance windows, I am aware that under Doze Google defers network requests, wake locks and so on. I just want the network request to be executed when Google allows).
Turns out it isn't Doze / Background execution limits. Either there is a bug in Android O or Huawei is doing some aggressive task management, that results in the listener being disconnected.
I've added some notification that listens when onDisconnected() gets executed. And on my device (Huawei Mate 10 Pro) with Android 8.0, onDisconnected() gets executed after a couple of minutes. However on my tablet which is running Android 7.0, the listener gets disconnected very few times after prolonged periods, however it instantly rebinds automatically and onConnected() gets executed.
As of now I have no clear indication if it's Android or Huawei.