Practical results of Android-service life cycle

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.