1. Concept:
(1). Service can be said to be a running activity in the background. It's not a separate process, it just needs to be applied to tell it what to do in the background.
(2). If it implements the interaction with the user, it needs to receive the display through the notification bar or by sending a broadcast.
(3). Its application is very extensive, especially in the frame layer, the application is more of the call to the system services.
2. Function:
(1). It is used to handle some background operations that do not interfere with the user's use. The following download, network access. Play music, which can be turned on by intent, and can also be bound to host objects (callers, for example, on activity) for use.
(2). If the activity is the information that displays the front page, then the service is operated in the background. If the service interacts with the foreground UI, it can be done by sending a broadcast or notification bar.
3. Life cycle:
(1). The life time of the service as a whole is from oncreate () to the start of the call, until the OnDestroy () method returns. As with activity, the service carries out its initialization in OnCreate (), releasing the remaining resources in the OnDestroy ().
(2). **startservice () in the manner of: **oncreate ()->onstartcommand ()->onstart ()->ondestroy ()
(3). * * Bindservice (): **oncreate ()->onbinder ()->onunbind ()->ondestroy (). The Onunbind () method ends when it returns.
4. Starting mode:
(1). Service cannot run itself, it needs to be invoked through an activity or other context object.
(2). There are two ways to start a service:
Context.startservice () and Context.bindservice () Start the service in two ways. If there is a time-consuming operation in the service's OnCreate () method or the OnStart () method, a new thread is opened. Start in both of these ways where you need a service.
Attention:
Usually use a lot of StartService method, you can put some time-consuming tasks into the background to deal with, when the processing is completed, you can broadcast or notify the bar to inform the front desk.
5. The following code to further understand:
(1). Mainactivity.java class:
Package com.example.servicetest;
Import Com.example.servicetest.service.MyService;
Import android.app.Activity;
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.view.View.OnClickListener;
Import Android.widget.Button; public class Mainactivity extends activity implements Onclicklistener {/** flag bit/private static String TAG = "Com.exam
Ple.servicetest.MainActivity ";
/** Start Service * * Private Button Mbtnstart;
/** Binding Service * * Private Button mbtnbind;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Initview ();
/** * Init the View */private void Initview () {Mbtnstart = (Button) Findviewbyid (R.id.startservice);
Mbtnbind = (Button) Findviewbyid (R.id.bindservice); Mbtnstart.setonclicklistener (This);
Mbtnbind.setonclicklistener (this); @Override public void OnClick View {switch (View.getid ()) {//Start service case R.id.startservice:starts
Ervice (New Intent (myservice.action));
Break
The way of binding services case R.id.bindservice:bindservice (new Intent (myservice.action), Conn, bind_auto_create);
Break
Default:break; } serviceconnection conn = new Serviceconnection () {public void onserviceconnected (componentname name, IBinder SE
Rvice) {log.v (TAG, "onserviceconnected");
public void onservicedisconnected (componentname name) {LOG.V (TAG, "onservicedisconnected");
}
};
@Override protected void OnDestroy () {Super.ondestroy ();
System.out.println ("-------OnDestroy ()-");
StopService (New Intent (myservice.action));
Unbindservice (conn);
}
}
(2). Myservice.java class:
Package com.example.servicetest.service;
Import Android.app.Service;
Import android.content.Intent;
Import Android.os.Binder;
Import Android.os.IBinder;
Import Android.util.Log; public class MyService extends Service {/** logo bit/private static String TAG = "Com.example.servicetest.service.MyServi
Ce ";
/** Behavior * * public static final String action = "Com.example.servicetest.service.MyService";
@Override public void OnCreate () {super.oncreate ();
SYSTEM.OUT.PRINTLN ("-----onCreate ()---");
@Override public void OnStart (Intent Intent, int startid) {Super.onstart (Intent, Startid);
SYSTEM.OUT.PRINTLN ("-----onStart ()---"); @Override public int Onstartcommand (Intent Intent, int flags, int startid) {System.out.println ("-----onstartcomma
nd ()---");
Return Super.onstartcommand (Intent, flags, Startid);
@Override public IBinder onbind (Intent arg0) {System.out.println ("-----onbind ()---");
return null; @Override public void Onrebind (Intent intent) {System.out.println ("-----onrebind ()---");
Super.onrebind (Intent);
@Override public boolean onunbind (Intent Intent) {System.out.println ("-----onunbind ()---");
return Super.onunbind (Intent);
@Override public void OnDestroy () {System.out.println ("-----ondestroy ()---");
Super.ondestroy ();
}
}
(3). Androidmanifest.xml
<!--registered-->
<service android:name= "Com.example.servicetest.service.MyService" >
< Intent-filter>
<!--intent--> <action android:name= to start a service
Com.example.servicetest.service.MyService "/>
<category android:name=" Android.intent.category.default " />
</intent-filter>
</service>
StartService Method:
(1). When the StartService button is pressed:
(2). When you continue to press the StartService button:
Bindservice Method:
(1). When the Bindservice button is pressed:
(2). When you continue to press the Bindservice button:
(3). Press the StartService button and press the Bindservice button first: