Service of Android four components

Source: Internet
Author: User
<span id="Label3"></p><p><p>In Android four components, service and activity two are similar, the difference is that activity for the foreground, service for the Background.</p></p><p><p>Use the service when your program does not need to render the interface with Components. A typical example is playing music, the interface will still need to play music after exiting, when the need is the service background to Run.</p></p><p><p><br></p></p><span style="font-family:SimHei"><span style="font-family:SimHei">Service Creation</span></span><span style="font-family:SimHei"><span style="font-family:SimHei">1. Define a subclass that inherits the service</span></span><span style="font-family:SimHei"><span style="font-family:SimHei">2. Configure the service in the Androidmainifest.xml File. </span></span><span style="font-family:SimHei"><span style="font-family:SimHei"> <u>is this process a little similar to creating activity? Right. </u></span></span><span style="font-family:SimHei"><span style="font-family:SimHei"><u><br></u></span></span><span style="font-family:SimHei"><span style="font-family:SimHei">service start and stop</span></span><span style="font-family:SimHei"><span style="font-family:SimHei">1, Call the context inside the definition of StartService (), StopService () two methods can Be. </span></span><span style="font-family:SimHei"><span style="font-family:SimHei">however, There is no communication and data exchange between the service and the visitor initiated in this way. </span></span><span style="font-family:SimHei"><span style="font-family:SimHei">2, The use of Bindservice (Intent service,serviceconnection conn,int Flags) and Unbindservice () start and close, you can achieve communication and data exchange. </span></span><span style="font-family:SimHei"><span style="font-family:SimHei"><br></span></span><span style="font-family:SimHei"><span style="font-family:SimHei">What is the principle of implementing communication and data exchange? </span></span><span style="font-family:SimHei"><span style="font-family:SimHei">let <span style="font-family:SimHei">'s take a look at Bindservice (Intent service,serviceconnection conn,int Flags) three parameters meaning:</span> </span></span><span style="font-family:SimHei"><span style="font-family:SimHei"><span style="font-family:SimHei">> Service: No doubt this is the service ready to start</span></span></span><span style="font-family:SimHei"><span style="font-family:SimHei"><span style="font-family:SimHei">> Conn: This is a serviceconnection object, his role is to judge the connection situation,</span></span></span><span style="font-family:SimHei">the <span style="font-family:SimHei"><span style="font-family:SimHei">onserviceconnected () method of the Serviceconnection object is recalled when the connection succeeds,</span></span></span><span style="font-family:SimHei">the <span style="font-family:SimHei"><span style="font-family:SimHei">onservicedisconnected () method of Serviceconnection is called back when there is a problem with the connection, or if an exception occurs, and this is not the Caller's active disconnection;</span></span></span><span style="font-family:SimHei"><span style="font-family:SimHei"><span style="font-family:SimHei">> Flags: If the service is created automatically at bind time (in case it is not yet created), you can set</span></span> the</span><span style="font-family:SimHei"><span style="font-family:SimHei"><span style="font-family:SimHei">0-not automatically created;</span></span></span><span style="font-family:SimHei"><span style="font-family:SimHei"><span style="font-family:SimHei">bind_auto_create-automatically created;</span></span></span><span style="font-family:SimHei"><span style="font-family:SimHei">  </span></span><span style="font-family:SimHei"><span style="font-family:SimHei"><br></span></span><span style="font-family:SimHei">The key to the <span style="font-family:SimHei">success of the connection is in the second argument, which has a IBinder object in the Onserviceconnected method of the parameter object, and the IBinder object, like the messenger of communication, is used to pass the Data. </span></span><span style="font-family:SimHei"><span style="font-family:SimHei">   </span></span><span style="font-family:SimHei"><span style="font-family:SimHei">Here is an example of Android crazy handout above:</span></span><span style="font-family:SimHei"><span style="font-family:SimHei">   </span></span><span style="font-family:SimHei">the <span style="font-family:SimHei">Onbind () in the service subclass is a method that must be implemented, which returns a IBinder object that communicates to the Caller. The returned IBinder object typically implements its own IBinder object with the inherited Binder.<br></span></span><pre name="code" class="java">public class Bindservice extends Service{private int Count;private Boolean Quit;//defines The object returned by the Onbinder method private Mybinder Bin Der = new Mybinder (); <span style= "background-color:rgb (102, 255, 153);" >//implements IBinder class <span style= "color: #333333 by inheriting binder;" >public class Mybinder extends Binder//①{public int getcount () {//get service running status: countreturn count;}} You must implement a method that binds the service when the callback method @overridepublic IBinder onbind (Intent Intent) {System.out.println ("Service is binded");// Return IBinder object return binder;} The </span></span>//service is created with the callback Method. @Overridepublic void OnCreate () {super.oncreate (); System.out.println ("Service is Created");//start a thread, dynamically modify the Count status value new thread () {@Overridepublic void run () {while (!quit) {try{thread.sleep (1000);} Catch (interruptedexception E) {}count++;}}}. Start ();} Callback this method when the Service is disconnected @overridepublic boolean onunbind (Intent Intent) {System.out.println ("service is unbinded"); Return true;} The method is recalled before the service is Closed. @Overridepublic void OnDestroy () {super.ondestroy (); this.quit =True System.out.println ("Service is destroyed");}}</pre><br><span style="font-family:SimHei"><span style="font-family:SimHei"><br></span></span><span style="font-family:SimHei">the <span style="font-family:SimHei">Following is an activity to bind the service, defining a Service's Mybinder class in the activity to access the Service's internal State. </span></span><span style="font-family:SimHei"><span style="font-family:SimHei"><br></span></span><pre name="code" class="java">public class Bindservicetest extends Activity{button bind, unbind, getservicestatus;// Maintain the IBinder object of the service being started Bindservice.mybinder Binder;<span style= "background-color:rgb (102, 255, 153);" >//defines a Serviceconnection object private serviceconnection conn = new Serviceconnection () {// Callback the method @overridepublic void onserviceconnected (componentname name, ibinder Service) when the activity and service connection succeeds { System.out.println ("--service connected--");//gets The Mybinder object returned by the Service's Onbind method binder = (bindservice.mybinder) Service ①}//callback when the activity is disconnected from the service @overridepublic void onservicedisconnected (componentname Name) { System.out.println ("--service disconnected--");}; </span> @Overridepublic void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main);//gets the start, stop, getservicestatus button in the program interface bind = (button) Findviewbyid (r.id.bind); Unbind = (button) Findviewbyid (r.id.unbind); getservicestatus = (button) Findviewbyid (r.id.getservicestatus);// Create a intentf that starts the serviceInal Intent Intent = new Intent ();//set Action property Intent for Intent.setaction ("org.crazyit.service.BIND_SERVICE"); Bind.setonclicklistener (new onclicklistener () {@Overridepublic void OnClick (View source) {///binding Specifies Serivcebindservice ( intent, conn, service.bind_auto_create);}); Unbind.setonclicklistener (new onclicklistener () {@Overridepublic void OnClick (View source) {// Unbind Serivceunbindservice (conn);}}); Getservicestatus.setonclicklistener (new onclicklistener () {@Overridepublic void OnClick (View source) {//get, and displays the Service's count value toast.maketext (bindservicetest.this, "the count value of serivce is:" + binder.getcount (), toast.length_short). Show (); ②}});}}</pre><br><span style="font-family:SimHei"><span style="font-family:SimHei">life cycle of service</span></span><span style="font-family:SimHei"><span style="font-family:SimHei">two different ways to start a service with different lifecycles</span></span><span style="font-family:SimHei"><span style="font-family:SimHei"><u><br></u></span></span><span style="font-family:SimHei"><span style="font-family:SimHei">non-binding service life cycle:</span></span><span style="font-family:SimHei"><span style="font-family:SimHei">onCreate ()->onstartcommand ()->ondestroy (</span> )</span><span style="font-family:SimHei"><span style="font-family:SimHei">Binding service Life Cycle:</span></span><span style="font-family:SimHei"><span style="font-family:SimHei">onCreate ()->onbind ()->onunbind ()->ondestroy (</span> )</span><span style="font-family:SimHei"><span style="font-family:SimHei"><u><br></u></span></span><span style="font-family:SimHei"><span style="font-family:SimHei"><br></span></span><p><p>Service of Android four components</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.