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.
Run a service in background continuously
Below code works for me...
public class AppService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, " MyService Created ", Toast.LENGTH_LONG).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, " MyService Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
}
Related Topics
Add a Header to a Gridview (Android)
Check Whether the Sd Card Is Available or Not Programmatically
Convert Magnetic Field X, Y, Z Values from Device into Global Reference Frame
Android: Determine Security Type of Wifi Networks in Range (Without Connecting to Them)
How to Dynamically Remove Items from Listview on a Button Click
Android - Volume Buttons Used in My Application
How to Style the Drawerarrowtoggle from Android Appcompat V7 21 Library
How to Restrict the Edittext to Accept Only Alphanumeric Characters
Google Firebase Sign Out and Forget User in Android App
How to Use Adb to Send Touch Events to Device Using Sendevent Command
Neither User 10102 Nor Current Process Has Android.Permission.Read_Phone_State
Getlastknownlocation Always Return Null After I Re-Install the APK File via Eclipse
How to Get Cell Service Signal Strength in Android