Preface:
The previous summary of the program to share data, you can use ContentProvider can also use sharedpreference, then how to share memory between processes? There is no shared memory between processes in the Android system, so there is a need to provide some mechanism for data communication between different processes.
To enable other applications to access the services provided by this application, the Android system is implemented using remote procedure calls (Procedure Call,rpc). Like many other RPC-based solutions, Android exposes service interfaces using an Interface definition language (Interface definition Language,idl). We know that 3 of the 4 Android application components (activity, broadcastreceiver, and ContentProvider) can be accessed across the process, while another Android application component service is also available. As a result, this service that can be accessed across processes can be called the Aidl (Android Interface Definition Language) service.
Then the actual combat to achieve the specific:
1.) First create a new Aidl file
Interface Itestinterface {
//Get process ID
int getprocessid ();
Process string
dealstring (string srcstring);
Strings appended with string
appendstring (string srcstring);
void Addperson (in person);
List<person> getpersons ();
}
Aidl Grammar Narration:
• Declare functions Basic and Java consistent, can pass parameters and return values, arguments and return values
• Parameters and return values the basic data types of the Java programming language (int, long, char, Boolean, and so on), string and Charsequence, collection interface type list and map, other Aidl interface types, Custom objects that implement the Parcelable interface
• Direction indicates that when transferring data using AIDL, it is not a string or charsequence type for a non-basic data type, that is, the parcelable type, requires directional instructions, including in, out, and inout.
Aidl only supports interface methods and cannot expose static variables.
2.) Service-Side Implementation interface
Private final Itestinterface.stub Mbinder = new Itestinterface.stub () {public
int getprocessid () {
log.e () Testservice "," Testservice Thread: "+ thread.currentthread (). GetName ());
LOG.E ("Testservice", "Testservice GetProcessID ()");
return Android.os.Process.myPid ();
}
Process string public string
dealstring (string srcstring)
{return
srcstring+srcstring;
}
String Append public
string appendstring (string srcstring)
{return
srcstring+srcstring;
}
3.) Client Access interface
Private Serviceconnection connection = new Serviceconnection () {@Override public void onservicedisconnected (C
Omponentname name) {LOG.E ("Testservice", "Testservice onservicedisconnected ()"); @Override public void onserviceconnected (componentname name, IBinder service) {itestinterface = Itestin Terface.
Stub.asinterface (service);
try {log.e ("Testservice", "Testservice onserviceconnected ()");
int Remoteid=itestinterface.getprocessid ();
LOG.E ("Testservice", "Testservice remoteid---->" +remoteid);
int currentpid = Android.os.Process.myPid ();
LOG.E ("Testservice", "Testservice currentpid---->" +currentpid);
LOG.E ("Testservice", "Testservice dealstring---->" +itestinterface.dealstring ("Remote Service");
LOG.E ("Testservice", "Testservice appendstring---->" +itestinterface.appendstring ("Remote Service");
catch (RemoteException e) {e.printstacktrace ();
}
}
};
4.) Call/Pass data through IPC
int Remoteid=itestinterface.getprocessid ();
LOG.E ("Testservice", "Testservice remoteid---->" +remoteid);
int currentpid = Android.os.Process.myPid ();
LOG.E ("Testservice", "Testservice currentpid---->" +currentpid);
LOG.E ("Testservice", "Testservice dealstring---->" +itestinterface.dealstring ("Remote Service");
LOG.E ("Testservice", "Testservice appendstring---->" +itestinterface.appendstring ("Remote Service");
5.) Service Declaration and binding/desorption
Statement:
<service
android:name= ". Testservice "
android:enabled=" true "
android:exported=" true "
android:label=" Remoteservice "
android:process= ": Remote" >
<intent-filter android:priority= "1000" >
<category android:name= " Android.intent.category.DEFAULT "/>
<action android:name=" Com.whoislcj.testaidl.TestService "/>"
</intent-filter>
</service>
Binding:
Intent Intent = new Intent ("Com.whoislcj.testaidl.TestService");
Intent.setpackage (Getpackagename ())//Here you need to set the name of the package you applied
bindservice (Intent, connection, context.bind_auto_ CREATE);
Uncoupling: Unbindservice (connection);
6.) Access rights are consistent with service
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.