Android - Periodic Background Service - Advice
Create a Service
to send your information to your server. Presumably, you've got that under control.
Your Service
should be started by an alarm triggered by the AlarmManager
, where you can specify an interval. Unless you have to report your data exactly every 30 minutes, you probably want the inexact alarm so you can save some battery life.
Finally, you can register your app to get the bootup broadcast by setting up a BroadcastReceiver
like so:
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
// Register your reporting alarms here.
}
}
}
You'll need to add the following permission to your AndroidManifest.xml
for that to work. Don't forget to register your alarms when you run the app normally, or they'll only be registered when the device boots up.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Continually Running Background Service
In oreo release Android defined limits to background services.
To improve the user experience, Android 8.0 (API level 26) imposes
limitations on what apps can do while running in the background.
Still if app need to run its service always, then we can create foreground service.
Background Service Limitations: While an app is idle, there are limits
to its use of background services. This does not apply to foreground
services, which are more noticeable to the user.
So create a foreground service. In which you will put a notification for user while your service is running. See this answer (There are many others)
Now what if you don't want a notification for your service. A solution is for that.
You can create some periodic task that will start your service, service will do its work and stops itself. By this your app will not be considered battery draining.
You can create periodic task with Alarm Manager, Job Scheduler, Evernote-Jobs or Work Manager.
- Instead of telling pros & cons of each one. I just tell you best. Work manager is best solution for periodic tasks. Which was introduced with Android Architecture Component.
- Unlike Job-Scheduler(only >21 API) it will work for all versions.
- Also it starts work after a Doze-Standby mode.
- Make a Android Boot Receiver for scheduling service after device boot.
I created forever running service with Work-Manager, that is working perfectly.
Android service periodically performing tasks in background?
You need to add а TimerTask, see how to do that here
How does one implement a background notification service in android when all services except for foreground ones get shut down after some time?
You have two choices:
1:Foreground Service.
You can keep a service running indefinitely, by promoting your service to a "foreground service". You can only become a foreground service by adding a Notification for the service to the notifications area. Presumably one which allows users to make your service go away. See Service.setForeground
.
- Poll periodically using
WorkManager
andAlarmManager
These APIs allow you to schedule periodic work when there is an active internet connection. The basic idea is that you would poll every few minutes to see whether there is stuff to be done.
There are no other options. This is by design. There is no way to lurk in the background constantly without displaying a notification. Android OS developers have put a lot of work into making sure that there is no other way.
Related Topics
Speech to Text from Own Sound File
Textbox Hidden Below Keyboard in Android Webview
Resizing Imageview to Fit to Aspect Ratio
Firebase Authentication Not Working in Signed APK
How to Get the Key from the Value in Firebase
How to Get the Dreaded Write_Secure_Settings Permission for My Android App
Android: How to Enable/Disable an Activity's Intent Filter Programmatically
How to Merge Two Mp3 Files into One (Combine/Join)
Firebase Dynamic Link Not Opening the App
Crop Video Before Encoding with Mediacodec for Grafika's "Continuous Capture" Activity
Given a Tensor Flow Model Graph, How to Find the Input Node and Output Node Names
Floatingactionbutton Doesn't Hide
How to Change the Group Owner in a Persistent Group in Wi-Fi Direct
Map Markers with Text in Google Maps Android API V2