The previous article introduced the activity as well as the use of intent, this article introduces the service. If the activity is compared to the foreground program, then the service is the background program, the service's entire lifecycle will only be performed in the background . service is called by intent as well as activity. To add a service in the project, create a new class that inherits the service, and then add it to the Service tab in the Androidmanifest.xml-> application->application nodes .
Service to be initiated by activity through StartService or bindservice, intent is responsible for passing parameters.
First paste out this program to run the screenshot as follows:
This article mainly explains the invocation of service, and its life cycle.
The image above is a StopService service status change after StartService.
The image above is a Unbindservice service status change after Bindservice.
StartService and Bindservice can start service, then what is the difference between them? The difference between them is to make the service cycle change. a service initiated by StartService must have stopservice to end the service, and not to invoke StopService will cause the activity to end and the service to run. Bindservice-initiated service can be terminated by Unbindservice, or automatically end after the activity ends (OnDestroy).
The image above is StartService after activity.finish () service status changes, service is still running.
The image above is bindservice after activity.finish () service status changes, the service finally automatic Unbindservice.
The Main.xml code is as follows:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " > <button android:layout_width= "wrap_content" android:layout_height= wrap_content "android:id=" @+id/ Btnstartmyservice "android:text=" Startmyservice "></Button> <button android:layout_width=" wrap_content "Android:layout_height=" wrap_content "android:id=" @+id/btnstopmyservice "android:text=" StopMyService "></" button> <button android:layout_width= "wrap_content android:layout_height=" wrap_content "android:id=" @+id/
Btnbindmyservice "android:text=" Bindmyservice "></Button> <button android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:id= "@+id/btnunbindmyservice" android:text= "UnbindMyService" >< /button> <button android:layout_width= "wrap_content" android:layout_height= "Wrap_content""Android:id=" @+id/btnexit "android:text=" Exit program ></Button> </LinearLayout>
The source code for the
Testservice.java is as follows:
Package com.testservice;
Import android.app.Activity;
Import Android.app.Service;
Import Android.content.ComponentName;
Import android.content.Intent;
Import android.content.ServiceConnection;
Import Android.os.Bundle;
Import Android.os.IBinder;
Import Android.util.Log;
Import Android.view.View;
Import Android.widget.Button; public class Testservice extends activity {Button Btnstartmyservice,btnstopmyservice,btnbindmyservice,
Btnunbindmyservice,btnexit;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Btnstartmyservice= (Button) This.findviewbyid (R.id.btnstartmyservice);
Btnstartmyservice.setonclicklistener (New Clickevent ());
Btnstopmyservice= (Button) This.findviewbyid (R.id.btnstopmyservice);
Btnstopmyservice.setonclicklistener (New Clickevent ());
Btnbindmyservice= (Button) This.findviewbyid (R.id.btnbindmyservice); Btnbindmyservice.setonclicklistener (New Clickevent());
Btnunbindmyservice= (Button) This.findviewbyid (R.id.btnunbindmyservice);
Btnunbindmyservice.setonclicklistener (New Clickevent ());
btnexit= (Button) This.findviewbyid (r.id.btnexit);
Btnexit.setonclicklistener (New Clickevent ());
@Override public void OnDestroy () {Super.ondestroy ();
LOG.E ("Activity", "OnDestroy"); Private Serviceconnection _connection = new Serviceconnection () {@Override public void onserviceconnected (compo Nentname arg0, IBinder arg1) {//TODO auto-generated method stub} @Override public void onservicedisconnected (Comp
Onentname name {//TODO auto-generated Method stub}}; Class Clickevent implements view.onclicklistener{@Override public void OnClick (View v) {Intent intent=new Intent (te
Stservice.this,myservice.class);
if (v==btnstartmyservice) {testService.this.startService (intent);
else if (v==btnstopmyservice) {testService.this.stopService (intent); else if (v==btnbindmyservICE) {TestService.this.bindService (intent, _connection, service.bind_auto_create); An else if (V==btnunbindmyservice) {if (myservice.servicestate== "Onbind")//service bind before it can be untied TestService.this.unbindSe
Rvice (_connection);
else if (v==btnexit) {testService.this.finish ();
}
}
}
}
Myservice.java source code is as follows:
Package com.testservice;
Import Android.app.Service;
Import android.content.Intent;
Import Android.os.IBinder;
Import Android.util.Log;
public class MyService extends Service {
static public String servicestate= "";
@Override public
IBinder onbind (Intent arg0) {
log.e ("Service", "Onbind");
Servicestate= "Onbind";
return null;
}
@Override Public
Boolean onunbind (Intent Intent) {
super.onunbind (Intent);
LOG.E ("Service", "Onunbind");
Servicestate= "Onunbind";
return false;
}
@Override public
void OnCreate () {
super.oncreate ();
LOG.E ("Service", "onCreate");
Servicestate= "OnCreate";
}
@Override public
void OnDestroy () {
Super.ondestroy ();
LOG.E ("Service", "OnDestroy");
Servicestate= "OnDestroy";
}
@Override public
void OnStart (Intent intent,int startid) {
Super.onstart (Intent, startid);
LOG.E ("Service", "OnStart");
Servicestate= "OnStart";
}