Understanding of how Bindservice is used in Android

Source: Internet
Author: User

Understanding of Bindservice mode use in Android-the only little God-Blog Park

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.

Bindservice and started service are service, where is the difference:

1. The Started service uses the StartService () method to make a call to the method, and there is no connection between the caller and the service, even if the caller exits, the services are still "onCreate ()-?" >onstartcommand ()->startservice ()->ondestroy () ", note that there is no onstart (), which is mainly replaced by the Onstartcommand () method, The OnStart method is not recommended for use.

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 () ".

Caller activity:

mainacitvity Package Com.zys.service;

Import Com.zys.service.BindService.MyBinder;

Import Android. R.bool;
Import android.app.Activity;
Import Android.content.ComponentName;
Import Android.content.Context;
Import android.content.Intent;
Import android.content.ServiceConnection;
Import Android.os.Bundle;
Import Android.os.IBinder;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;

Public class mainactivity extends Activity {
Private Button startbtn;
Private Button stopbtn;
Private Boolean Flag;
/** Called when the activity is first created. */
@Override
Public void onCreate (Bundle savedinstancestate) {
Super . OnCreate (Savedinstancestate);
Setcontentview (R.layout.main);
Flag = false ;
// Set
startbtn = (Button) This . Findviewbyid (R.ID.STARTBTN);
STOPBTN = (Button) This . Findviewbyid (R.ID.STOPBTN);
Startbtn.setonclicklistener (listener);
Stopbtn.setonclicklistener (listener);
}

Private Onclicklistener Listener = New Onclicklistener () {

@Override
Public void OnClick (View v) {
// TODO auto-generated Method Stub
Switch (V.getid ()) {
Case r.id.startbtn:
Bindservice ();
Break ;
Case r.id.stopbtn:
UnBind ();
Break ;
default :
Break ;
}
}


};

Private void Bindservice () {
Intent Intent = New Intent (mainactivity. This , Bindservice. class );
Bindservice (Intent, Conn, context.bind_auto_create);
}

Private void UnBind () {
if (Flag == true ){
Unbindservice (conn);
Flag = false ;
}
}

Private Serviceconnection Conn = New serviceconnection () {

@Override
Public void onservicedisconnected (componentname name) {
// TODO auto-generated Method Stub

}

@Override
Public void onserviceconnected (componentname name, IBinder service) {
// TODO auto-generated Method Stub
Mybinder Binder = (mybinder) service;
Bindservice Bindservice = Binder.getservice ();
Bindservice.mymethod ();
Flag = true ;
}
};

}

Service Bindservice

Bindservice Package Com.zys.service;

Import Java.io.FileDescriptor;

Import Android.app.Service;
Import android.content.Intent;
Import Android.os.Binder;
Import Android.os.IBinder;
Import Android.os.IInterface;
Import Android.os.Parcel;
Import android.os.RemoteException;
Import Android.util.Log;

Public class Bindservice extends Service {

Private Static Final String TAG = " Bindservice " ;

Public void MyMethod () {
LOG.I (TAG, " Bindservice-->mymethod () " );
}

@Override
Public IBinder Onbind (Intent Intent) {
// TODO auto-generated Method Stub
return Mybinder;
}

Public class Mybinder extends binder{

Public Bindservice GetService () {
return Bindservice. This ;
}
}

Private Mybinder Mybinder = New Mybinder ();
}

????? 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.

The method process is as follows:

New Bindservice object Intent Intent = new Intent (mainactivity.this,bindservice.class)

->bindservice (Intent, Conn, context.bind_auto_create);->onbind () function? -----Passing Mybinder Objects------->onserviceconnected ()

?--> Gets the Bindservice object that was just corresponding to the binder object by passing the binder object-and invokes the method defined in the service.

??? 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.

Understanding of how Bindservice is used in Android

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.