Android calls between different processes AIDL implementation of communication services
recent calls to different processes in Aidl Android have made some attempts to make calls between different applications:The steps are as follows:1: First write the service services in the program to be called
The specific code is as follows:
Package Com.jiaruihua.service.demo;import Android.app.service;import Android.content.intent;import Android.os.binder;import Android.os.ibinder;public class Alipayservice extends Service {@Overridepublic IBinder onbind (Intent Intent) {//TODO auto-generated method stubSystem.out.println ("Remote service is bound"); <span style= "color: #ff0000;" >//note here to return to our ibinder</span>return new Mybinder ();} @Overridepublic boolean onunbind (Intent Intent) {//TODO auto-generated method stubSystem.out.println ("Remote service has been lifted"); return Super.onunbind (intent);} @Overridepublic void OnCreate () {//TODO auto-generated method stubSystem.out.println ("remote service created"); Super.oncreate ();} @Overridepublic void OnDestroy () {//TODO auto-generated method stubSystem.out.println ("Remote service destroyed"); Super.ondestroy (); /*
*<span style= "color: #ff0000;" > Here is the method to be called by remote, function processing here </span> * Payment Operation */public void Paymoney () {System.out.println ("payment operation");} /*
* Here we need a iservice.stub, which is what we define ISERVICE.AIDL, implemented, see step two * Iservice.stub has implemented Binder */public class Mybinder extends <span style= "color: #ff0000;" >iservice.stub</span>{@Overridepublic void Callmethodservice () {Paymoney ();}}}
2: Create a Iservice.aidl file required in the previous step
The code is as follows:
The package Com.jiaruihua.service.demo;//aidl interface definition does not require the access modifier default public interface IService {void Callmethodservice ();}
The Iservice.java file is automatically created in the Gen directory in eclipse
3: Register in the Mainfest.xml file IService
4: Create a package in another program that needs to be called, note that the package name here is consistent with the specified package name of the service in the mainfest.xml above;Let's copy the Iservice.aidl file created by the second step above .
5: Call Remote service
Package Com.example.mygameapp;import Com.jiaruihua.service.demo.iservice;import Android.app.activity;import Android.app.service;import Android.content.componentname;import Android.content.intent;import Android.content.serviceconnection;import Android.os.bundle;import Android.os.ibinder;import Android.os.remoteexception;import Android.view.view;import Android.view.view.onclicklistener;public Class Mainactivity extends Activity {private Intent intent;private iservice iservice; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); intent = new Intent (); <span style= "color: #ff0000;" >//the action here must be one to </span>intent.setaction ("Com.jiaruihua.ServicePay") that you set in the calling program; Bind service Findviewbyid (r.id.banding). Setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) { <span style= "color: #ff0000;" >//Bind Service Open service </span>bindservice (intent, New MyConn (), bind_auto_create);}); &lT;span style= "color: #ff0000;" >//calls remote method </span>findviewbyid (R.id.callmethod). Setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated method stubtry {iservice.callmethodservice ();} catch (RemoteException e) { TODO auto-generated catch Blocke.printstacktrace ();}});} Private class MyConn implements Serviceconnection {//successfully bound @overridepublic void onserviceconnected (componentname name, IBinder service) {<span style= "color: #ff0000;" >//convert SERCICSE to IService interface type IService = Iservice.Stub.asInterface (service); </span> @Overridepublic void onservicedisconnected (componentname name) {//TODO auto-generated Method stub}}}
Source Demo
Android calls between different processes AIDL implementation of communication services