Keep Service running
You should create a sticky service. Read more about it here.
You can do this by returning START_STICKY in onStartCommand.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " + startId + ": " + intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
Read also about application:persistent which is "Whether or not the application should remain running at all times". This is more troublesome - System will try not to kill your app which will effect others in the system, you should be careful using it.
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.
Creating a server, techniques to keep a service running (or blocked?)
There are various libraries that help you roll your own Windows/Unix service in Java:
- Apache Commons Daemon
- Akuma
- Java Service Wrapper
How you keep the application running depends on the actual needs of your application. If your application is a server, you would normally want to listen for incoming connections which usually involves some sort of blocking/polling. How you do that again depends on the type of server you want to build. Of the generic solutions there's the ServerSocket class and its method accept() that you already mentioned. Another possibility is to use java.nio and implement a reactor which provides a single-threaded server that can handle multiple connections at once (see http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf for details), but may be a bit hard to grasp and debug.
Related Topics
Android - Navigation View Item Menu Background Color
How to Add "Android Design Support Library" to Eclipse with Adt-Plugin
Is Deprecated Word the Only Difference Between Fill_Parent and Match_Parent
Android SQLite Selection Args[] with Int Values
Android Facebook Authorization - Can Not Log in When Official Facebook App Is Installed
How to Read/Write a Boolean When Implementing the Parcelable Interface
Android: What Is Better - Multiple Activities or Switching Views Manually
Android Cursor with Ormlite to Use in Cursoradapter
Android Programming: Where to Start for Creating a Simple File Browser
Post on Facebook Wall Using Facebook Android Sdk Without Opening Dialog Box
Get Last Inserted Value from SQLite Database Android
How to Show Text in Android System Status Bar
Notifydatasetchanged Not Working on Recyclerview
Ndk: How Include *.So Files in Androidstudio
Checkbox Gets Unchecked on Scroll in a Custom Listview
Swap Fragment in an Activity via Animation