After the interface exits the process program is still running, will not be killed, such as music advertising device, background download, etc.
Note: This article only discusses started mode
Main.xml Code Analysis
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" > <ButtonAndroid:id= "@+id/btnstartservice"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Start Service" /> <ButtonAndroid:id= "@+id/btnstopservice"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Stop Service" /></LinearLayout>
Create a class Exampleservice.java
PackageCom.szy.service;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.os.IBinder;ImportAndroid.util.Log; Public classExampleserviceextendsservice{Private Static FinalString tag= "Exampleservice"; @Override Public voidonCreate () {log.i (TAG,"Exampleservice-->oncreate"); Super. OnCreate (); } @Override Public voidOnStart (Intent Intent,intStartid) { //TODO auto-generated Method Stub Super. OnStart (Intent, Startid); } @Override Public voidOnDestroy () {log.i (TAG,"Exampleservice-->ondestroy"); Super. OnDestroy (); } @Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {log.i (TAG,"Exampleservice-->onstartcommand"); returnStart_not_sticky; } @Override Publicibinder onbind (Intent Intent) {return NULL; }}
Main object class Mainactivity.java
PackageCom.szy.service;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classMainactivityextendsactivity{PrivateButton Btnstartservice; PrivateButton Btnstopservice; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); Btnstartservice=(Button) Findviewbyid (R.id.btnstartservice); Btnstopservice=(Button) Findviewbyid (R.id.btnstopservice); Btnstartservice.setonclicklistener (listener); Btnstopservice.setonclicklistener (listener); } PrivateOnclicklistener listener=NewOnclicklistener () { Public voidOnClick (View v) {Intent Intent=NewIntent (mainactivity. This, Exampleservice.class); Switch(V.getid ()) { CaseR.id.btnstartservice:startservice (Intent); Break; CaseR.id.btnstopservice:stopservice (Intent); Break; default: Break; } } };}
Define it in Androidmanifest.xml.
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.szy.service"Android:versioncode= "1"Android:versionname= "1.0"> <USES-SDKandroid:minsdkversion= "8" /> <ApplicationAndroid:icon= "@drawable/icon"Android:label= "@string/app_name"> <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name"> <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> <ServiceAndroid:name=". Exampleservice " /> </Application></Manifest>
Android Learning Service (1)--->started way