There are several ways to implement an Android interprocess communication instance:
1,aidl
2,broadcast
3,intent
4,content Provider
Record the way Aidl implements communication between Android processes:
Android Aidl interprocess communication Implementation steps:
One: Create Aidl server: Aidl files, automatically generated interface Java files, service services
Xxxinterface.aidl (interface definition for remote services)
Package Com.example.aidltest;interface imyaidlinterface { String getValue (); An interface method for the Aidl service that calls the Aidl service to invoke the method }
1. Create a xxxinterface.aidl file that is in the form of an interface
2,eclipse automatically completes, according to your xxxinterface.aidl file, generates a Xxxinterface.java file in Gen, really creates an interface in Xxxinterface, and creates an inner stub (extends Android.os.Binder)
Aidltestservice.java (Implementation of the remote Service specific interface method)
Package Com.example.aidltest;import Android.app.service;import Android.content.intent;import android.os.IBinder; Import Android.os.remoteexception;public class Aidltestservice extends Service {//two CHOICES://1: inner class implementation, Create an inner class that inherits from Imyaidltest.stub (automatically generated by an imyaidltest inner Class)//2: Anonymous class implementation, creating an Imyaidltest.stub object, implementing an interface method string GetValue ()// 1, inner class private Myaidlimp aidlimp=new Myaidlimp ();p ublic class Myaidlimp extends imyaidlinterface.stub{@ Overridepublic String GetValue () throws RemoteException {//TODO auto-generated method Stubreturn "Hello world!";}} 2, anonymous class private final imyaidlinterface.stub Mbinder = new Imyaidlinterface.stub () {public String getValue () throws remoteexception {return "Hello zero!";};}; @Overridepublic ibinder onbind (Intent arg0) {//TODO auto-generated method stub//returns this implementation GetValue return Mbinderreturn Aidlimp;}}
3, create Aidl service (Aidltestservice extends services)
4, create a Xxxinterface.stub object in Serveice, which can be implemented through an inner class or an anonymous class. This object implements all the specific interface methods
5, Xxxinterface.stub object created in return 4 in Onbind ()
Androidmanifest.xml
<service android:name= "Aidltestservice" > <intent-filter> <action android:name= " Com.example.aidltest.AIDLtestService "/> </intent-filter> </service>
6, declare the service, add the action in Intent-filter
Two: Client Invoke service
The remote Xxxactivity.java
</pre><p><pre name= "code" class= "java" >package com.example.aidltest;import android.app.Activity; Import Android.app.actionbar;import Android.app.fragment;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.os.remoteexception;import Android.view.LayoutInflater; Import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.button;import Android.widget.textview;import Android.os.build;public class Mainactivity extends Activity {private Button btn;p rivate TextView tv;//Create Imyaidlinterface object private Imyaidlinterface myaidlinterface=null;//Create serviceconnection Object private Serviceconnection sc=new serviceconnection () {@Overridepublic void onservicedisconnected (ComponentName arg0) {//TODO auto-generated method Stub} @Overridepublic void onserviceconnected (componentname arg0, IBinder arg1) {//TODO auto-generated method stubmyaidlinterface = IMy AidlInterface.Stub.asInterface (ARG1);}}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); btn= (Button) Findviewbyid (R.id.button1); tv= (TextView) Findviewbyid (R.ID.TEXTVIEW1);// Create a service intent, start the service intent intent = new Intent (This,aidltestservice.class); Bindservice (Intent, Sc,context.bind_auto_ CREATE); Btn.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated Method Stubtry {//Call remote service via Imyaidlinterface object string s= myaidlinterface.getvalue (); System.out.println (s); Tv.settext (s);} catch (RemoteException e) {//TODO auto-generated catch Blocke.printstacktrace ();}});}}
1, create the Xxxinterface object MyInterface, create the Serviceconnection object SC, instantiate the Xxxinterface object in the SC onserviceconnected method.
SC is used in later bindservice.
MyInterface is the object after which the service process is invoked.
2, create a service intent, use this intent to start the remote server's service---aidltestservice
3,bindservice (Intent, sc,context.bind_auto_create);
4, call the remote service via MyInterface.
Aidl Implementing an Android inter-process communication instance