Step: Establish server-side service, expose interface
1.BinderService
1 /**2 * @Title Binderservice.java3 * @package Cn.boxai.binderservice4 * @since 5 * @version1.0.06 * @authorVic Lee7 * @date, 2016-3:50:04 PM8 */9 PackageCn.boxai.binderservice;Ten One ImportAndroid.app.Service; A Importandroid.content.Intent; - ImportAndroid.os.Binder; - ImportAndroid.os.IBinder; the - Public classBinderserviceextendsService { - - //step:1 difind Mbinder +IBinder mbinder=NewMybinder ();//Step:3 implements Mbinder - + A //step:2 difine Mybinder and extends at classMybinderextendsBinder - { -
Call Other services
public binderservice getService ()
{
Returnbinderservice.this;
}
26 public string HelloWorld (string name) 27 {
28 return "you name is:" +name; 29 30< /span> 31
//Add other services
public void HelloService ()
{
for (int i = 0; i <; i++)
{
try {
Thread.Sleep (1000);
LOG.I ("HelloService", i+ "");
} catch (Interruptedexception e)
{
E.printstacktrace ();
}
}
}
+ // step:4 retrue Mbinder Exposed interface - @Override Public ibinder onbind (Intent Intent) { return Mbinder; Panax Notoginseng - }
2. Client
1 PackageCn.boxai.binderservice;2 3 ImportCn.boxai.binderservice.BinderService.MyBinder;4 ImportAndroid.os.Bundle;5 ImportAndroid.os.IBinder;6 Importandroid.app.Activity;7 ImportAndroid.content.ComponentName;8 Importandroid.content.Intent;9 Importandroid.content.ServiceConnection;Ten ImportAndroid.util.Log; One ImportAndroid.view.Menu; A ImportAndroid.view.View; - ImportAndroid.view.View.OnClickListener; - ImportAndroid.widget.Button; the ImportAndroid.widget.Toast; - - Public classBinderserviceactivityextendsActivity { - Button Mbinderservicebutton; + @Override - protected voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.activity_main); atmbinderservicebutton=(Button) Findviewbyid (r.id.btn_binderservice); -Mbinderservicebutton.setonclicklistener (NewOnclicklistener () - { - - @Override - Public voidOnClick (View v) in { - //step 5:call interface from server to //Bindservice parameter=>_intent +Intent _intent=NewIntent (binderserviceactivity. This, Binderservice.class); - Bindservice (_intent, Conn, binderservice.bind_auto_create); the } * }); $ Panax Notoginseng } - //Step 6:bindservice parameter=>conn the PrivateServiceconnection conn=Newserviceconnection () + { A the @Override + Public voidonservicedisconnected (componentname name) { - $ //TODO auto-generated Method Stub $ - } - the @Override - Public voidonserviceconnected (componentname name, IBinder service) {Wuyi the //Step 7:receive Server Method -String bindername= ((mybinder) service). HelloWorld ("LD")); WuToast.maketext (binderserviceactivity. This, BinderName, 3000). Show (); -LOG.I ("Binderservice", "binderservice==>" +bindername);
( (Mybinder) service). GetService (). HelloService ();//bind other services About } $ }; - - - A +}
Unbind
Client
Button Munbinderservicebutton; // Unbind munbinderservicebutton=(Button) Findviewbyid (r.id.btn_unbinderservice); Munbinderservicebutton.setonclicklistener (new onclicklistener () { @Override Public void OnClick (View v) { unbindservice (conn); } });
Service End-end
@Override publicboolean onunbind (Intent Intent) { // TODO auto-generated Method stub return Super . Onunbind (intent); }
Android Binderservice Process