from the operating mode, the Android service component not running in a separate process or thread , but is running in the main thread of the process just as with other Android components. Therefore, synchronizing the time-consuming operations directly in the serviced component will block the main thread.
like the subclass of service Intentservice, complete the constructor and Onhandleintent (Intent Intent) method, and then you can send the Execute command through StartService (Intent). When the Intentservice receives the first command, Intentservice completes the boot, triggers a background thread , and then puts the command into the queue. Intentservice executes these commands sequentially and calls Onhandleintent (intent) methods on the background thread for each command at the same time. When all the commands in the queue are executed, the service is stopped and destroyed. Ps:intentservice is a non-sticky service.
non-sticky Service: When the service itself thinks that it has completed the task, it calls the Stopself () or stopself (int) method to tell the Android that the task is complete to end the service.
Sticky Service: The service continues to run until a component calls Context.stopservice (Intent) to stop it.
Life cycle of Service
First, Context.startservice
When the service is first started, OnCreate () and OnStart () are called in turn, and if the service is already created, call StartService (intent)
Only the OnStart () will be executed. The parameters passed by intent can be processed in the Onstartcommand () method. Execution at the end of the initiative
stopself () method, the passive end calls the Context.stopservice () method, and then executes the OnDestroy ().
Second, Context.bindservice
If the client wants to continue to connect to the service, get a service instance that can use the binding service this way.
Similarly, when the service is first created, OnCreate () is called, and the Onbind method constructs a IBinder object to return to the caller, establishing a communication connection
Then, the caller can obtain this IBinder object by overriding the Onserviceconnected () method in the Serviceconnection class. Pass
Onunbind unbind the service and then executes OnDestroy (). The component of the binding service should be unbound before it is destroyed, otherwise it will cause a memory leak
Terrace
Some of the code is as follows:
Service side
public class MyService extends Service {private static final String TAG = "MyService"; Private Mybinder Mbinder = new Mybinder (); Creates a binder object, which is returned to the caller via Onbinder, and the caller obtains the service instance through this object @Override public ibinder onbind (Intent Intent) {LOG.E ( TAG, "Start ibinder~~~"); return mbinder; } @Override public void OnCreate () {LOG.E (TAG, "Start oncreate~~~"); Super.oncreate (); } @Override public void OnDestroy () {LOG.E (TAG, "Start ondestroy~~~"); Super.ondestroy (); } @Override public boolean onunbind (Intent Intent) {log.e (TAG, "Start onunbind~~~"); return Super.onunbind (Intent); }//Create a public method for the service that is used to test the publicly String () {System.out.println ("test"); } public class Mybinder extends binder{myservice getService () {return myservice.this; } } }
Call End:
public class Servicedemo extends Activity { private serviceconnection mserviceconnection = new Serviceconnection () { c1/>public void onserviceconnected (componentname name, IBinder service) { Mmyservice = ((Myservice.mybinder) Service). GetService (); Mmyservice.test (); Public method for invoking the service instance directly } publicly void onservicedisconnected (componentname name) { } }; public void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.main); Intent I = new Intent (); I.setclass (Servicedemo.this, myservice.class); Mcontext.bindservice (i, mserviceconnection, bind_auto_create); } }
Android Service Notes