1. Inheriting the service class, overriding the Onbinder () method of the method service
2. Register the service in manifest, set the filtered URL (public static final String ACTION = "Com.lql.service.ServiceDemo";//Custom)
3. Start Service StartService (new Intent (servicedemo.acition));
<service android:name= "Com.example.androidtest.ServiceDemo" >
<intent-filter>
<action android:name= "Com.lql.service.ServiceDemo"/>
</intent-filter>
</service>
4. Summary: Service life cycle
First boot:
04-09 21:46:09.637:i/servicedemo (9280): onCreate
04-09 21:46:09.637:i/servicedemo (9280): Onstartcommand
04-09 21:46:09.637:i/servicedemo (9280): OnStart
Second boot:
04-09 21:46:49.369:i/servicedemo (9413): Onstartcommand
04-09 21:46:49.369:i/servicedemo (9413): OnStart
Servicedemo.java
public class Servicedemo extends Service {private static final String TAG = "Servicedemo"; public static final String ACTION = "Com.lql.service.ServiceDemo"; @Overridepublic ibinder onbind (Intent arg0) {//TODO auto-generated method Stubreturn null;} @Overridepublic void OnCreate () {//TODO auto-generated method stublog.i (TAG, "onCreate"); Super.oncreate ();} @Overridepublic void OnDestroy () {//TODO auto-generated method stublog.i (TAG, "OnDestroy"); Super.ondestroy ();} @Override @deprecatedpublic void OnStart (Intent Intent, int startid) {//TODO auto-generated method stublog.i (TAG, "OnStar T "); Super.onstart (Intent, Startid);} @Overridepublic int Onstartcommand (Intent Intent, int flags, int startid) {//TODO auto-generated method stublog.i (TAG, "O Nstartcommand "); return Super.onstartcommand (Intent, flags, Startid);} }
Startservice.java
public class StartService extends Activity { Button start_service; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_start_service); Start_ Service = (Button) Findviewbyid (R.id.start_service); Start_service.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View arg0) {//TODO auto-generated method Stubstartservice (New Intent (servicedemo.action));}} );}}
Start_service_activity.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". StartService "> <textview android:id=" @+id/textview1 "android:layout_width=" Wrap_content "an droid:layout_height= "Wrap_content" android:text= "@string/hello_world"/> <button android:id= "@+id/s Tart_service "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:layout _below= "@+id/textview1" android:layout_centerhorizontal= "true" android:layout_margintop= "110DP" Andro id:text= "Open Service"/></relativelayout>
Service Simple Instance