"turn" understanding of how Bindservice is used in Android
Source: Internet
Author: User
<span id="Label3"></p><p><p>Original Url: http://www.cnblogs.com/onlylittlegod/archive/2011/05/15/2046652.html</p></p><p><p>Recently learned about the application of the service in android, in the bindservice part of the small card, mainly started not thoroughly understand why to achieve This.</p></p><p><p>Bindservice and started service are service, where is the difference:</p></p><p><p>1. The StartService () method is used in the Started service to make a call to the method, and there is no connection between the caller and the service, even if the caller exits, the service is still in progress "onCreate ()->onstartcommand () StartService ()->ondestroy () ", Note that there is no onstart (), mainly by the Onstartcommand () method to replace, the OnStart method is not recommended to Use.</p></p><p><p>2. Bindservice uses the Bindservice () method to bind the service, the caller and the binder are bound together, and the caller terminates "onCreate ()->onbind ()->onunbind () once it exits the Service. OnDestroy () ".</p></p><p><p>Caller Activity:</p></p><p><p>Mainacitvity</p></p><p><p>Package com.zys.service;</p></p><p><p>Import com.zys.service.BindService.MyBinder;</p></p><p><p>Import Android. r.bool;<br>Import android.app.Activity;<br>Import android.content.ComponentName;<br>Import android.content.Context;<br>Import android.content.Intent;<br>Import android.content.ServiceConnection;<br>Import android.os.Bundle;<br>Import android.os.IBinder;<br>Import android.view.View;<br>Import android.view.View.OnClickListener;<br>Import android.widget.Button;</p></p><p>public class Mainactivity extends Activity {<br>Private Button startbtn;<br>Private Button stopbtn;<br>Private Boolean flag;<br>/** called when the activity is first Created. */<br>@Override<br>public void OnCreate (Bundle Savedinstancestate) {<br>Super.oncreate (savedinstancestate);<br>Setcontentview (r.layout.main);<br>Flag = false;<br>Set up<br>STARTBTN = (Button) This.findviewbyid (r.id.startbtn);<br>STOPBTN = (Button) This.findviewbyid (r.id.stopbtn);<br>Startbtn.setonclicklistener (listener);<br>Stopbtn.setonclicklistener (listener);<br>}<br> <br>Private Onclicklistener listener = new Onclicklistener () {<br> <br>@Override<br>public void OnClick (View V) {<br>TODO auto-generated Method Stub<br>Switch (v.getid ()) {<br>Case R.id.startbtn:<br>Bindservice ();<br>Break<br>Case R.id.stopbtn:<br>UnBind ();<br>Break<br>Default<br>Break<br>}<br>}<br> <br> <br>};<br> <br>private void Bindservice () {<br>Intent Intent = new Intent (mainactivity.this,bindservice.class);<br>Bindservice (intent, conn, context.bind_auto_create);<br>}<br> <br>private void UnBind () {<br>if (flag = = True) {<br>Unbindservice (conn);<br>Flag = false;<br>}<br>}<br> <br>Private Serviceconnection conn = new Serviceconnection () {<br> <br>@Override<br>public void onservicedisconnected (componentname Name) {<br>TODO auto-generated Method Stub<br> <br>}<br> <br>@Override<br>public void onserviceconnected (componentname name, ibinder Service) {<br>TODO auto-generated Method Stub<br>Mybinder Binder = (mybinder) service;<br>Bindservice Bindservice = Binder.getservice ();<br>Bindservice.mymethod ();<br>Flag = true;<br>}<br>};<br> <br>}</p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span>Package com.zys.service;<br><br>Import com.zys.service.BindService.MyBinder;<br><br>Import Android. r.bool;<br>Import android.app.Activity;<br>Import android.content.ComponentName;<br>Import android.content.Context;<br>Import android.content.Intent;<br>Import android.content.ServiceConnection;<br>Import android.os.Bundle;<br>Import android.os.IBinder;<br>Import android.view.View;<br>Import android.view.View.OnClickListener;<br>Import android.widget.Button;<br><br>public class Mainactivity extends Activity {<br>Private Button startbtn;<br>Private Button stopbtn;<br>Private Boolean flag;<br>/** called when the activity is first Created. */<br>@Override<br>public void OnCreate (Bundle Savedinstancestate) {<br>Super.oncreate (savedinstancestate);<br>Setcontentview (r.layout.main);<br>Flag = false;<br>Set up<br>STARTBTN = (Button) This.findviewbyid (r.id.startbtn);<br>STOPBTN = (Button) This.findviewbyid (r.id.stopbtn);<br>Startbtn.setonclicklistener (listener);<br>Stopbtn.setonclicklistener (listener);<br>}<br> <br>Private Onclicklistener listener = new Onclicklistener () {<br> <br>@Override<br>public void OnClick (View V) {<br>TODO auto-generated Method Stub<br>Switch (v.getid ()) {<br>Case R.id.startbtn:<br>Bindservice ();<br>Break<br>Case R.id.stopbtn:<br>UnBind ();<br>Break<br>Default<br>Break<br>}<br>}<br> <br> <br>};<br> <br>private void Bindservice () {<br>Intent Intent = new Intent (mainactivity.this,bindservice.class);<br>Bindservice (intent, conn, context.bind_auto_create);<br>}<br> <br>private void UnBind () {<br>if (flag = = True) {<br>Unbindservice (conn);<br>Flag = false;<br>}<br>}<br> <br>Private Serviceconnection conn = new Serviceconnection () {<br> <br>@Override<br>public void onservicedisconnected (componentname Name) {<br>TODO auto-generated Method Stub<br> <br>}<br> <br>@Override<br>public void onserviceconnected (componentname name, ibinder Service) {<br>TODO auto-generated Method Stub<br>Mybinder Binder = (mybinder) service;<br>Bindservice Bindservice = Binder.getservice ();<br>Bindservice.mymethod ();<br>Flag = true;<br>}<br>};<br> <br>}<span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Service Bindservice</p></p><p><p>Bindservice</p></p><p><p>Package com.zys.service;</p></p><p><p>Import java.io.FileDescriptor;</p></p><p><p>Import android.app.Service;<br>Import android.content.Intent;<br>Import android.os.Binder;<br>Import android.os.IBinder;<br>Import android.os.IInterface;<br>Import android.os.Parcel;<br>Import android.os.RemoteException;<br>Import android.util.Log;</p></p><p><p>public class Bindservice extends Service {</p></p><p><p>private static final String TAG = "bindservice";</p></p><p><p>public void MyMethod () {<br>LOG.I (TAG, "bindservice-->mymethod ()");<br>}<br><br>@Override<br>Public IBinder Onbind (Intent Intent) {<br>TODO auto-generated Method Stub<br>Return mybinder;<br>}<br><br>public class Mybinder extends binder{<br><br>Public Bindservice GetService () {<br>Return bindservice.this;<br>}<br>}<br><br>Private Mybinder Mybinder = new Mybinder ();<br>}</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span>Package com.zys.service;<br><br>Import java.io.FileDescriptor;<br><br>Import android.app.Service;<br>Import android.content.Intent;<br>Import android.os.Binder;<br>Import android.os.IBinder;<br>Import android.os.IInterface;<br>Import android.os.Parcel;<br>Import android.os.RemoteException;<br>Import android.util.Log;<br><br>public class Bindservice extends Service {<br><br>private static final String TAG = "bindservice";<br><br>public void MyMethod () {<br>LOG.I (TAG, "bindservice-->mymethod ()");<br>}<br> <br>@Override<br>Public IBinder Onbind (Intent Intent) {<br>TODO auto-generated Method Stub<br>Return mybinder;<br>}<br> <br>public class Mybinder extends binder{<br> <br>Public Bindservice GetService () {<br>Return bindservice.this;<br>}<br>}<br> <br>Private Mybinder Mybinder = new Mybinder ();<br>}<span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Since the service in Android uses the Onbind method to bind services, return a IBinder object to operate, and we want to get the content of the specific service method, we need to IBinder object to return to the specific service object in order to operate , so that the specific service object must first implement the binder object, this way we can use the Bindservice method to bind the service, obtain the binder object after the specific service object, Then get the methods in the service and so On. So what we need to note is that the bindservice way to bind the service gets must be the object that implements the binder, so this is the way we have to use binder to get the service instead of directly using the service class, This is the internal implementation of Android Constraints.</p></p><p><p>The method process is as Follows:</p></p><p><p>New Bindservice object Intent Intent = new Intent (mainactivity.this,bindservice.class)</p></p><p><p>->bindservice (intent, conn, context.bind_auto_create);->onbind () function-----pass mybinder object-------> Onserviceconnected ()</p></p><p><p>--by passing the Binder object get just the Bindservice object corresponding to the binder Object--call the method defined in the Service.</p></p><p><p>This must be done through a binder object, because it is passed through a binder object, gets the service object through the binder object, and then gets the required services, so the server must implement binder for delivery and Use.</p></p><p><p>"turn" understanding of how Bindservice is used in Android</p></p></span>
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