Typically, companies need to keep their products alive for as long as possible in the user's phone, including not being affected by large numbers, manually clearing the background, and so on. Here one way is to double process guard;
Model:
Two service establishes an IPC communication by Aidl, that is to bind each other in the Onstartcommand method of the two service by Aidl Way;
For example, in S1:
@Override publicint int int startid) { this . Bindservice (new Intent ( This, LocalService2. Class), Conn, context.bind_important); return start_sticky; }
In the Onbind:
@Override public ibinder onbind (Intent Intent) { // TODO: Return the communication channel to the service. return mybinder; }
And Mybinder is the concrete realization of aidl:
Private classMybinderextendsiprocessconnection.stub {@Override Public voidBasictypes (intAnint,LongAlong,BooleanAboolean,floatAfloat,DoubleAdouble, String astring)throwsremoteexception {} @Override Public voidGetprocessname ()throwsremoteexception {log.i ("Process", "LocalService1"); } }
This establishes the connection in two serivce, and through the Conn this serviceconnection to listen to the connection situation, whether the disconnection, the disconnection means that one side is killed
Private class extends serviceconnection { @Override publicvoid onserviceconnected (componentname Name, IBinder service) { } @Override publicvoid onservicedisconnected (componentname name) { } }
When disconnected, it will callback onservicedisconnected this method, the inside restart another serivce and bind he can guarantee two service to listen to each other, guard each other.
About the Android app KeepAlive