Unfortunately this method does not work any more in Android 11 because you cannot restart a foreground process from a background process.I have already found the solution. I hope to be able to update the blog soon.
However there is a new blog entry for that. Please see this link
In a previous post, I explained how to create a never ending background service. That solution worked for Android up to Version 7. After that, that method has started misbehaving on an increasing number of vendor specific Android implementations (e.g. Xiaomi) and then finally most of the vendors.
So it is time to give an update.
In order to understand this post, you must understand the previous one as I will build on that code.
The part that does not work any more in Android > 7 is the sequence Service dying calling the Broadcast receiver which in turn will recreate the server. Since Android 7 (but officially since Lollipop!), any process called by a BroadcastReceiver is run at low priority and hence eventually killed by Android. So the service will end up being killed. Depending on the specific vendor implementation, this may happen in some days (e.g. Samsung) or in some hours (e.g. Xiaomi).
In order to solve this issue we must create the service in the BroadcastReceiver in a way that keeps high priority. The suggested way to do it is by use of a JobService. A JobService guarantees that the process will finish. JobServices are cool because they allow also to set a number of conditions attached (e.g. running only if some conditions are true e.g. there is internet connection). We can also require that the job service is restarted if the phone is rebooted.
So the code to make the magic happens is the following. In the Broadcaster Receiver, we will add the following code:
As you will see, if the Android version is greater or equal than Lollipop (minimum requirement for JobServices), instead of calling the service creator right away, we go through the JobService. We schedule the job.
Note here two parameters:
• setOverrideDeadline(0) which is a dummy constraint asking to start immediately (at least one is required)
• setPersisted(true) which requires to restart the Job in case the phone is rebooted. It is really not necessary in our code as the Broadcast will receive a message when the phone is rebooted but good to have
The code for the Job Service is:
Note here two parameters:
• setOverrideDeadline(0) which is a dummy constraint asking to start immediately (at least one is required)
• setPersisted(true) which requires to restart the Job in case the phone is rebooted. It is really not necessary in our code as the Broadcast will receive a message when the phone is rebooted but good to have
The code for the Job Service is:
it will invoke the creation of the service (via the class ProcessMainClass and will register the receiver ready for any request of restart. It will have a method that is called when Android kills the Job Service (it may happen). This method is in charge of requesting to restart itself via the Broadcast Receiver and after a while afterwards will deregister the receiver (by delaying a bit to avoid unregistering before the receiver receives the message.
There are some additions here and there to the previous code (and I am afraid I may have changed the names of some classes) but the sense should be clear.
That is it. It will work on more recent version of Android now.
Note that there are far more moving parts in the code, so make sure to check the complete code up. It is provided below.
The code
The code is provide here; licence: MIT, so you can use it for any purpose.
There are some additions here and there to the previous code (and I am afraid I may have changed the names of some classes) but the sense should be clear.
That is it. It will work on more recent version of Android now.
Note that there are far more moving parts in the code, so make sure to check the complete code up. It is provided below.
The code
The code is provide here; licence: MIT, so you can use it for any purpose.