In fact, Android cannot go long and will definitely not be killed by the background. We can only try to use some operations to improve the chance of not being killed.
1. onStartCommand method, return START_STICKY
START_STICKY After the service process is killed after running onStartCommand, it will remain in the starting state, but those incoming intents are not retained. The service will try to recreate again soon, because it remains in the starting state, and onstartCommand will be called after the service is created. If no start command is passed to the service, the null intent will be obtained.
START_NOT_STICKY After the service process is killed after running onStartCommand, no new intent is passed to it. The Service will be moved out of the Start State and will not be recreated until a new obvious method (startService) call. Because if no undecided intent is passed, the service will not start, that is, onstartCommand will not receive any null intents during this period.
START_REDELIVER_INTENT After the service process is killed after running onStartCommand, the system will start the service again and pass in the last intent to onstartCommand. The intent is not stopped passing until stopSelf(int) is called. If there is still an unprocessed intent after being killed, the service will still start automatically after being killed. Therefore onstartCommand will not receive any null intents.
2. Improve service priority
In the file, the highest priority can be set through the property android:priority = "1000" for intent-filter. 1000 is the highest value. If the number is smaller, the priority will be lower. It is also suitable for broadcasting.
3. Improve the priority of service process
Processes in Android are managed. When the system process space is tight, processes will be automatically recycled according to priority. Android divides processes into 6 levels, and they are from high to low in order of priority:
1.Foreground process (FOREGROUND_APP)
2.Visitable process (VISIBLE_APP)
3. Secondary service process (SECONDARY_SERVER )
4. Background process (HIDDEN_APP)
5. Content supply node (CONTENT_PROVIDER)
6. Empty process (EMPTY_APP)
When the service is running in a low memory environment, some existing processes will be killed. Therefore, the priority of the process will be important, and you can use startForeground to put the service into the foreground state. This will have a lower chance of being killed when there is low memory.
4. Send broadcast in onDestory to restart service
service +broadcast The method is to send a custom broadcast when the service goes ondestory, and restart the service when the broadcast is received;
5. Application plus Persistent attribute
6. Monitoring system broadcast to determine the service status
Monitor and capture it through some broadcasts of the system, such as: mobile phone restart, interface wake-up, application status change, etc., and then determine whether our Service is still alive, don’t forget to add permissions.
7. Dual-process Service
Let two processes protect each other. After one of the Service is cleaned up, the process that has not been cleaned can restart the process immediately.
8. Contact the manufacturer and add to the whitelist
The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!