If the activity is likened to a foreground program, then the service can be seen as a daemon. The service is called by intent as well as activity.
In the project you want to add a service, create a new class that inherits the service, and go to Androidmanifest.xml-application
Add the service tag in the.
As follows:
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.service"Android:versioncode= "1"Android:versionname= "1.0" > <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" /> <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <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=". MyService "> <Intent-filter> <ActionAndroid:name="" /> </Intent-filter> </Service> </Application></Manifest>
Where the first name is the service location, including the full package name and service name, if the package name is the package name that you defined, that is, the name of the package under the Gen directory, the direct ". Service name" is available. The second name is Intent.setaction () when you call the service, which you can define by yourself.
The code is as follows:
Activity_main.xml:
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/btnstartmyservice"Android:text= "Startmyservice"> </Button> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/btnstopmyservice"Android:text= "Stopmyservice"> </Button> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/btnbindmyservice"Android:text= "Bindmyservice"> </Button> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/btnunbindmyservice"Android:text= "Unbindmyservice"> </Button> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/btnexit"Android:text= "Exit program"> </Button></LinearLayout>
Myservice.java:
PackageCom.example.service; ImportAndroid.app.Service; Importandroid.content.Intent; ImportAndroid.os.IBinder; ImportAndroid.util.Log; Public classMyServiceextendsService {Static PublicString servicestate= ""; @Override Publicibinder onbind (Intent arg0) {LOG.E ("Service", "Onbind"); ServiceState= "Onbind"; return NULL; } @Override Public BooleanOnunbind (Intent Intent) {Super. Onunbind (Intent); LOG.E ("Service", "Onunbind"); ServiceState= "Onunbind"; return false; } @Override Public voidonCreate () {Super. OnCreate (); LOG.E ("Service", "OnCreate"); ServiceState= "OnCreate"; } @Override Public voidOnDestroy () {Super. OnDestroy (); LOG.E ("Service", "OnDestroy"); ServiceState= "OnDestroy"; } @Override Public voidOnStart (Intent Intent,intStartid) { Super. OnStart (Intent, Startid); LOG.E ("Service", "OnStart"); ServiceState= "OnStart"; } }
Mainactivity.java:
PackageCom.example.service; Importandroid.app.Activity; ImportAndroid.app.Service; ImportAndroid.content.ComponentName; Importandroid.content.Intent; Importandroid.content.ServiceConnection; ImportAndroid.os.Bundle; ImportAndroid.os.IBinder; ImportAndroid.util.Log; ImportAndroid.view.View; ImportAndroid.widget.Button; Public classMainactivityextendsActivity {Button btnstartmyservice,btnstopmyservice,btnbindmyservice,btnunbindmyservice,btnexit; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Btnstartmyservice= (Button) This. Findviewbyid (R.id.btnstartmyservice); Btnstartmyservice.setonclicklistener (Newclickevent ()); Btnstopmyservice= (Button) This. Findviewbyid (R.id.btnstopmyservice); Btnstopmyservice.setonclicklistener (Newclickevent ()); Btnbindmyservice= (Button) This. Findviewbyid (R.id.btnbindmyservice); Btnbindmyservice.setonclicklistener (Newclickevent ()); Btnunbindmyservice= (Button) This. Findviewbyid (R.id.btnunbindmyservice); Btnunbindmyservice.setonclicklistener (Newclickevent ()); Btnexit= (Button) This. Findviewbyid (R.id.btnexit); Btnexit.setonclicklistener (Newclickevent ()); } @Override Public voidOnDestroy () {Super. OnDestroy (); LOG.E ("Activity", "OnDestroy"); } PrivateServiceconnection _connection =Newserviceconnection () {@Override Public voidonserviceconnected (componentname arg0, IBinder arg1) {//TODO auto-generated Method Stub} @Override Public voidonservicedisconnected (componentname name) {//TODO auto-generated Method Stub } }; classClickeventImplementsview.onclicklistener{@Override Public voidOnClick (View v) {Intent Intent=NewIntent (mainactivity. This, MyService.class); if(v==Btnstartmyservice) {mainactivity. This. StartService (Intent); } Else if(v==Btnstopmyservice) {mainactivity. This. StopService (Intent); } Else if(v==Btnbindmyservice) {mainactivity. This. Bindservice (Intent, _connection, service.bind_auto_create); } Else if(v==Btnunbindmyservice) { if(myservice.servicestate== "Onbind")//Service is bound before it can be untiedMainactivity. This. Unbindservice (_connection); } Else if(v==btnexit) {mainactivity. This. Finish (); } } } }
Android Development Service