Previously recorded cross-application binding service:http://blog.csdn.net/gaopeng0071/article/details/46049929, then how to communicate after the binding, we will learn below.
The first step,
You need to modify the Aidl in the Service1 project to add a method.
package com.example.service1.aidl; interface IMyService { void basicType(); void setName(String name);}
The setname is used to store the name method.
Then clear project
Step Two,
At this point, the Onbind method in our service class needs to implement the new interface.
PackageCom.example.service1;ImportAndroid.app.Service;ImportAndroid.content.Intent;ImportAndroid.os.IBinder;ImportAndroid.os.RemoteException;ImportCom.example.service1.aidl.IMyService; Public class myservice extends Service { PrivateString ServiceName ="Default name";Private BooleanRunning@Override PublicIBinderOnbind(Intent Intent) {//TODO auto-generated method stub return NewImyservice.stub () {@Override Public void Basictype()throwsremoteexception {//TODO auto-generated method stub}@Override Public void SetName(String name)throwsremoteexception {serviceName = name; } }; }@Override Public void onCreate() {running =true;NewThread () { Public void Run() { while(running) {Try{Thread.Sleep ( +); }Catch(Interruptedexception e) {//TODO auto-generated catch blockE.printstacktrace (); } System.out.println (ServiceName); } }; }.start ();Super. OnCreate (); }@Override Public void OnDestroy() {Super. OnDestroy (); running =false; }}
The code 27 to 29 lines, receives the name and puts in the global variable, provides to the OnCreate method output.
The third step,
Copy the Aidl from the Service1 project to the Service2 project, and the package names are consistent,
Fourth Step,
Modify Service2 Apply activity layout, add a text field, and a button. Used to submit the information in text to the service in the Service1 project.
Fifth Step,
Modify the activity in the SERVICE2 project to increase communication with Service1,
PackageCom.example.service2;ImportCom.example.service1.aidl.IMyService;Importandroid.app.Activity;ImportAndroid.content.ComponentName;ImportAndroid.content.Context;ImportAndroid.content.Intent;ImportAndroid.content.ServiceConnection;ImportAndroid.os.Bundle;ImportAndroid.os.IBinder;ImportAndroid.os.RemoteException;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.TextView; Public class mainactivity extends Activity implements Onclicklistener , serviceconnection {Intent serviceintent;PrivateImyservice Imyservice =NULL; TextView T;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Serviceintent =NewIntent (); Serviceintent.setcomponent (NewComponentName ("Com.example.service1","Com.example.service1.MyService")); Findviewbyid (R.id.button1). Setonclicklistener ( This); Findviewbyid (R.id.button2). Setonclicklistener ( This); Findviewbyid (R.id.button3). Setonclicklistener ( This); Findviewbyid (R.id.button4). Setonclicklistener ( This); Findviewbyid (R.ID.BUTTON5). Setonclicklistener ( This); t = (TextView) Findviewbyid (R.ID.TEXTVIEW1); }@Override Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.main, menu);return true; }@Override Public void OnClick(View v) {Switch(V.getid ()) { CaseR.id.button1:startservice (serviceintent); Break; CaseR.id.button2:stopservice (serviceintent); Break; CaseR.id.button3:bindservice (Serviceintent, This, context.bind_auto_create); Break; CaseR.id.button4:unbindservice ( This); Imyservice =NULL; Break; CaseR.ID.BUTTON5:if(Imyservice! =NULL) {Try{Imyservice.setname (T.gettext (). toString ()); }Catch(RemoteException e) {//TODO auto-generated catch blockE.printstacktrace (); } } } }@Override Public void onserviceconnected(componentname name, IBinder service) {Imyservice = IMyService.Stub.asInterface (service); System.out.println ("onserviceconnected"); SYSTEM.OUT.PRINTLN (service); }@Override Public void onservicedisconnected(componentname name) {//TODO auto-generated method stub}}
Code 72 lines, transferring data
Code 84 line Usage Imyservice = IMyService.Stub.asInterface (service);
Run the results,
Android-bind service and communicate across apps