Basically everyone knows that increasing the service priority can largely keep your service from being killed due to insufficient memory, but the system simply kills the lower priority at this point, assuming that the memory is not enough and will kill your service.
1.android:persistent= "true"
The resident memory attribute is not valid for third-party apps. Here is the official note
Android:persistent
Whether or not the application should remain running @ all times-' true ' if it should, and ' false ' if not. The default value is "false". Applications should not normally set this flag; Persistence mode is a intended only for certain system applications.
2.startForeground place it in the foreground
Notification Notification = new Notification (); Notification.flags = notification.flag_ongoing_event; Notification.flags |= notification.flag_no_clear; Notification.flags |= Notification.flag_foreground_service;
3. Ability to monitor intent.action_time_tic system clock broadcasts. The system sends this broadcast every once in a while. When the service is killed, the broadcast starts over a period of time
Dynamic BrochureAndroid.intent.action.TIME_TICK Monitoring
Infer whether the service is started
public boolean isservicerunning (String serviceName) {Activitymanager manager = (Activitymanager) getsystemservice ( Context.activity_service); For (Runningserviceinfo service:manager.getRunningServices (integer.max_value)) { if (servicename.equals ( Service.service.getClassName ())) { return true; } } return false;}
After receiving the broadcast, infer whether to start the service if it does not start it
if (Intent.getaction (). Equals (Intent.action_time_tick)) {if (!isservicerunning (name)) {Intent mintent = new Intent (context, myservice.class); Context.startservice (mintent); } }
Three ways to get your service out of "one-click acceleration" and system kill