Android Periodic work even with app killed - android

I would like to start my foreground service every 30 minutes to synchornize data with server.
What I have in mind
Setup a periodic task that just starts my foreground synchronization service.
What I have tried so far
Broadcast Receiver with android.net.conn.CONNECTIVITY_CHANGE. This was my first approach. Try to synchronize data only if device lost connection sometime and recovered it later on. This doesn't work anymore for newer android versions. When app is killed, this broadcast receiver stop working.
AlarmManager
AndroidX.Work.Worker
JobService
All what I tried stop working after app killed. So my question is the following. Is there a way of achieving what I'm aming for in android 10 or above?

Related

How to run long live background service on Android?

I have an application which is for searching colleges, and is running a background service to listen the server for updates, new messages, califications, etc. But in the newest versions of Android (10 and above) the service finish when the user close the app (in the foreground and background).
The question is: what Kind of service or worker (or something) can I implement to listen the server for real time notifications despite the app being closed?
What I tried: service, intent service, workmanager. This solutions finishes when the app is kill. And also foreground service (actually i'm doing this) that works well but has to show fixed icon to notify the user the service is working, and when the app have to show a new notificacion for other reason it won't change (it's like the foreground would have a exclusive priority).
What I don't want: do 'tricks' like send broadcast to another class and restart the service or trigger alarm manager. This things not work and if maybe works cant get so far.

BroadcastReceiver that working even when the app is closed

I have a question about BroadcastReceivers. I have an app that needs to receive constantly the ACTION_TIME_TICK intent to refresh the clock widgets. I implemented a foreground service which contains a BroadcastReceiver that listens to that action. But on android oreo the only way to start a service is starting it as foreground service which implies to show a persistent notification to the user which notify that the app is working in background.
I don't want to show to the user that notification. I tried to implement BroadcastReceiver inside the Application class. But this is not working proprely. Somethimes the BroadcastReceiver doesn't work and somethimes works well. Even when it works well, if the app is removed from the recent tasks, the BroadcastReceiver stops to work. Is there a way to use a BroadcastReceiver that works indefinitely without the use of a service?
Is there a way to use a BroadcastReceiver that works indefinitely without the use of a service?
In the case of ACTION_TIME_TICK: No, since you cannot use a manifest-declared BroadcastReceiver.
Since you're not willing to use a foreground service, you may want to look into START_STICKY. In this situation, that's the only way to maximize your Service's run-time: Allow the system to re-start it, capriciously, after it's been stopped for lack of resources. Of course, this means your clock could stop updating for arbitrarily long periods of time.
Note that your problem is not limited to Oreo (Oreo does not require you to be a foreground service, but it does impose certain limits on what you're allowed to do if you're NOT in the foreground). Even on earlier OSs, if you're not foreground, the system considers you a prime candidate for elimination.

Android Oreo killing background services and clears pending alarms, scheduled jobs after entering doze mode

My app has a background service running that gets users current location and update it to a server every five minutes. To run this location update process continuously, I use alarm manager to set its next execution time from the service itself. However, when I install the app in my Nokia 6 running Android 8.1 it works for some time and if I keep the phone idle for some time, my service will get killed with the next alarms by the application also being cleared from system alarm manager. My guess was that the idle time makes the phone enter doze mode. However, I don't understand why the alarm managers got cleared. To my understanding, the doze mode should open up maintenance windows periodically to execute any pending tasks.
To mitigate this issue, I tried to apply a JobScheduler service on top of AlarmManager, which runs every 15 minutes. Purpose of this jobscheduler was to re-start the service which has the alarmmanager in it, so even if it gets killed and the alarm is cleared, jobscheduler would re-up the service.
After I tested this patch and keeping it for some time to go into idle mode, it resulted in getting both JobScheduler Service and Service which has the alarm in it killed with the scheduled jobs and alarms getting cleared from the system.
It is said in the Android documentation that we can use JobScheduler to mitigate its background execution limitations. And to test this out I forced killed the two services when I tested the app, but the already scheduled job did not get cleared, and it made the service with the alarm run again successfully. I don't understand the reason for this behavior, although the Evernote guys give an explanation that could match this scenario in here Android Job by Evernote
Any ideas for this abnormal behavior?
Test Environment Details
Device : Nokia 6 (TA-1021)
OS : Android 8.1.0
You would not be able to run background services long running in Oreo as there are behaviour changes, now Oreo to optimise system memory, battery etc, it kills background service, to solve your issue you should use foreground service.
Have a look at Background execution limits https://developer.android.com/about/versions/oreo/android-8.0-changes
A suggestion from me, if you can use FCM then go for it, becasue apps like WeChat, Facebook uses it, to deliver notifications and they don't face any problem...
Hope this helps in understanding the issue....
In Doze more, the alarms do not get reset, but get deferred to a later time. You have two mainstream options here:
Use either setAndAllowWhileIdle() or setExactAndAllowWhileIdle(). However, these too can fire at the maximum frequency of 1 time per 9 minutes. So you'll have to decrease the frequency at which you get location in your app.
Use a foreground service by way of showing a foreground notification. Everyone does that (apps like Uber, Google Maps etc). That way, your service won't get killed, and be treated as though you have an app open.
I'm currently facing the same issue and doing the same workaraound like you do. That is, setting the Jobscheduler to a periodic job to launch my Foreground Service every 15 min in case it is getting killed for whatever reasons like a killed task. This works like a charm on pre Oreo Versions.
For Oreo the only solution I am awared of at the moment is, to allow the app to autostart in the settings. Under installed apps that is. Then it should work like pre Oreo again.
What Ive heard but not tested yet, is to set the setPersisted(true) option in the Job Scheduler.
Let me know if that helps
I assumed currently DOZE mode not allowed to background service so you need to find a way that DOZE mode will not affect on your app.To solve your issue you should use foreground service. or make some battery setting. Any way my better option is you should go with Firebase Cloud Messaging

Android app does not work in doze mode

I'm building an application on Android Nougat, which will work constantly (in active mode and in a doze mode (when phone is sleep)).
The problem is that after about 6 hours of inactivity Android stops the application.
There is a UI with activity and a service. The service uses bluetooth, network and recording to a local database. Approximately once per minute, bluetooth works and the result is recorded in the local database.
After that, the data in the database is sent over the network.
The service runs for 6 hours and after that its work stop. The notification is displayed but the service does not work.
The manager.GetRunningServices method also indicates that the service is started but there are no signs of service.
I tried everything you can:
The service uses the method StartForeground
used wakeLock - does not help
used the whitelist - prolongs for a couple of hours, but in the end, all the same, the service stops working
Used the AlarmManager. It works constantly, but the code in OnReceive indicates that the service is running, but in fact it is not.
I started the service in a separate process so that he would use as little memory as possible, but this also does not help.
Maybe someone has ideas about this?
Thank you.
you can restart the service after 6 hours using the job scheduler. periodically restart your app around 6 hours.

Android Service run indefenitely

I notice that applications like Skype use a service which basically runs 24x7, without getting killed at all. You cannot even manually kill it using task killers ( you can kill them by going to running services and kill service ). How is this implemented?
I find that in Android 2.3, my service gets killed after running for sometime. onDestroy() is never called even if I start the service with START_STICKY. However this works fine on my 2.1 device, that is the service doesnt get killed.
Thanks
How is this implemented?
Based on the Skype screenshots that show a Notification icon, then they are most likely using startForeground().
I find that in Android 2.3, my service gets killed after running for sometime.
That is perfectly normal.
First, most Android applications do not really need a service that "basically runs 24x7". Users do not like such services, which is why task killers and the Running Services screen and the auto-kill logic in the OS exist. The only reason a service should be running "24x7" is if is delivering value every microsecond. VOIP clients, like Skype, will deliver value every microsecond, as they are waiting for incoming phone calls. Most Android applications do not meet this criterion.
If your service is running constantly, but for a user-controlled period (e.g., a music player), startForeground() is a fine solution.
Otherwise, I would rather that you find a way to eliminate the service that "basically runs 24x7", switching to a user-controllable polling system using AlarmManager, so your service is generally not in memory except when it is delivering value.

Categories

Resources