Life cycle Practices for StartService:
Public classMyServiceextendsService {@Nullable @Override Publicibinder onbind (Intent Intent) {return NULL; } @Override Public voidonCreate () {Super. OnCreate (); LOG.I ("MSG", "OnCreate"); } @Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {LOG.I ("MSG", "Onstartcommand"); return Super. Onstartcommand (Intent, flags, Startid); } @Override Public voidOnDestroy () {LOG.I ("MSG", "OnDestroy"); Super. OnDestroy (); }}
There are now two activity,mainactivity, uselessactivity,mainactivity have a button to start uselessactivity,uselessactivity start Myservier
1.MainActivity start uselessactivity, uselessactivity start MyService
Log.i=msg:oncreate Msg:onstartcommand
① if StopService (Intent) is not called in uselessactivity:
Press RETURN to Mainactivity, will not be played Log.i=msg:ondestroy. ★ The service will not be destroyed along with the activity that initiated it. ★
Then press back to exit the program, and then open the program, start the service, hit Log.i=onstartcommand. Instead of playing msg:oncreate, the service is still in ★ services will not be destroyed with the program that initiated it. ★
Then press back to exit the program, and in the phone's program list to remove the program, and then open the program, start the service, hit Log.i=onstartcommand. Without hitting msg:oncreate, the service is still ★ The service will not be destroyed because the program that initiated it is destroyed. ★
② if the OnDestroy in uselessactivity calls StopService (Intent):
Uselessactivity:
@Override protected void OnDestroy () { Super.ondestroy (); StopService (intent); }
Press back to mainactivity and hit Log.i=msg:ondestroy. ★ The service will be destroyed if you need to call StopService (Intent). ★
Life cycle Practice of bindservice: (Slowly replenished)
Practical results of Android-service life cycle