Android Service Introduction and startup shutdown mode

Source: Internet
Author: User

(1) service is one of the four components of Android system, and activity is the same level of components; it is a kind of service that has a long life cycle, no visual interface, and runs in the background. For example, we can listen to music using service, Downloads can use the service, we can also use it to record changes in our geographical location, or to start a service to always listen to some kind of action, we can also use the service update content Provider, send intent and boot system notifications, etc.;

  

(2) The service life cycle is not too complex, it only inherits the OnCreate (), Onstartcommand (), OnDestroy () method; It starts in two ways:

  Context.startservice ()->oncreate ()->onstartcommand ()->service running--call Context.stopservice ( )->ondestroy ();

  Context . Bindservice ()->oncreate ()->onbind ()->service running--call >onunbind (), OnDestroy ()

Note that: the original OnStart () method has been deprecated, has been replaced by Onstartcommand (), but Onstartcommand () is the first call OnStart ();

Life cycle

Examples are as follows:

Click Startservicebutton: More than a few clicks;

When you click Stopservicebutton;

Activity_main.xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/linearlayout01"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >     <ButtonAndroid:id= "@+id/startbutton"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Startservicebutton"        />    <ButtonAndroid:id= "@+id/stopbutton"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Stopservicebutton"        /></LinearLayout>

Firstservice.java

 PackageCom.xiaozhang.androidservice;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.os.IBinder; Public classFirstServiceextendsService {@Override Publicibinder onbind (Intent Intent) {System.out.println ("Service onbind!"); return NULL; } @Override Public voidonCreate () {Super. OnCreate (); System.out.println ("Service onCreate ();"); } @Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {System.out.println ("Startid" +Startid); System.out.println ("Service Onstartcommand ()"); return Super. Onstartcommand (Intent, flags, Startid); } @Override Public voidOnDestroy () {System.out.println ("Service ondestory ()"); Super. OnDestroy (); }}

Mainactivity.java

 PackageCom.xiaozhang.androidservice;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classMainactivityextendsActivity {PrivateButton Startbutton =NULL; PrivateButton Stopbutton =NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Startbutton=(Button) Findviewbyid (R.id.startbutton); Stopbutton=(Button) Findviewbyid (R.id.stopbutton); Startbutton.setonclicklistener (NewStartservicelistener ()); Stopbutton.setonclicklistener (NewStopservicelistener ()); }    classStartservicelistenerImplementsOnclicklistener {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (); Intent.setclass (mainactivity. This, FirstService.class);        StartService (Intent); }    }    classStopservicelistenerImplementsOnclicklistener {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (); Intent.setclass (mainactivity. This, FirstService.class);        StopService (Intent); }    }}

Finally, you need to register the service with the Androidmanifest.xml:

<android:name= ". FirstService "></service>

Part of the content is transferred from:

Http://www.cnblogs.com/zhangdongzi/archive/2012/01/08/2316711.html

Recommended reading:

http://blog.csdn.net/ithomer/article/details/7364024

Android Service Introduction and startup shutdown mode

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.