One, the code
1.xml
(1) Main.xml
1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3android:orientation= "Vertical"4Android:layout_width= "Fill_parent"5android:layout_height= "Fill_parent"6>7<Button8Android:id= "@+id/startservice"9Android:layout_width= "Fill_parent"Tenandroid:layout_height= "Wrap_content" Oneandroid:text= "StartService" A/> -<Button -Android:id= "@+id/stopservice" theAndroid:layout_width= "Fill_parent" -android:layout_height= "Wrap_content" -android:text= "StopService" -/> +</LinearLayout>
(2) Androidmanifest.xml
1<?xml version= "1.0" encoding= "Utf-8"?>2<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"3 Package= "Com.service"4Android:versioncode= "1"5Android:versionname= "1.0" >6 7<uses-SDK8Android:minsdkversion= "8"9Android:targetsdkversion= "/>"Ten One<Application AAndroid:allowbackup= "true" -android:icon= "@drawable/ic_launcher" -Android:label= "@string/app_name" theAndroid:theme= "@style/apptheme" > -<Activity -Android:name= ". Testactivity " -Android:label= "@string/app_name" > +<intent-filter> -<action android:name= "Android.intent.action.MAIN"/> + A<category android:name= "Android.intent.category.LAUNCHER"/> at</intent-filter> -</activity> -<service android:name= ". FirstService "/> -</application> - -</manifest>
2.java
(1) Testactivity.java
1 PackageCom.service;2 3 Importandroid.app.Activity;4 Importandroid.content.Intent;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.View;7 ImportAndroid.view.View.OnClickListener;8 ImportAndroid.widget.Button;9 Ten Public classTestactivityextendsActivity { One /**Called when the activity is first created.*/ A PrivateButton Startservicebutton =NULL; - PrivateButton Stopservicebutton =NULL; - the @Override - Public voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.main); +Startservicebutton =(Button) Findviewbyid (r.id.startservice); -Startservicebutton.setonclicklistener (NewStartservicelistener ()); +Stopservicebutton =(Button) Findviewbyid (r.id.stopservice); AStopservicebutton.setonclicklistener (NewStopservicelistener ()); atSystem.out.println ("Activity onCreate"); - } - - classStartservicelistenerImplementsOnclicklistener { - @Override - Public voidOnClick (View v) { inIntent Intent =NewIntent (); -Intent.setclass (testactivity. This, FirstService.class); to StartService (intent); + } - } the * classStopservicelistenerImplementsOnclicklistener { $ @OverridePanax Notoginseng Public voidOnClick (View v) { -Intent Intent =NewIntent (); theIntent.setclass (testactivity. This, FirstService.class); + StopService (intent); A } the } +}
(2) Firstservice.java
1 PackageCom.service;2 3 ImportAndroid.app.Service;4 Importandroid.content.Intent;5 ImportAndroid.os.Binder;6 ImportAndroid.os.IBinder;7 8 Public classFirstServiceextendsService {9 Ten @Override One Publicibinder onbind (Intent Intent) { A //TODO auto-generated Method Stub -System.out.println ("Service onbind"); - return NULL; the } - - //when a Servcie object is created, the function is called First - @Override + Public voidonCreate () { - //TODO auto-generated Method Stub + Super. OnCreate (); ASystem.out.println ("Service onCreate"); at } - - @Override - Public intOnstartcommand (Intent Intent,intFlagsintStartid) { - //TODO auto-generated Method Stub -SYSTEM.OUT.PRINTLN ("Flags--->" +flags); inSystem.out.println ("Startid--->" +Startid); -System.out.println ("Service Onstartcommand"); to returnStart_not_sticky; + } - the @Override * Public voidOnDestroy () { $ //TODO auto-generated Method StuboPanax NotoginsengSystem.out.println ("Service ondestory"); - Super. OnDestroy (); the } +}
Android_mars Study Notes _s01 original version _016_service