2nd Chapter IPC Mechanism

Source: Internet
Author: User

turn on multi-process and negative impactIPC: Inter-process communication. A thread is an inclusive relationship to a process.
There are only 1 ways to use: To assign the Android:process property to four components in manifest, you cannot assign a thread or entity class the process where the runtime is located.
 
   
  
  1. android:process=":remote"
Processes with a process name that begins with ":" belong to a private process, and other applied components cannot run in the same process as it did, while the process name does not begin with ":" As a global process, and other applications can run in the same process with it in the Shareuid way.
UID: Each app assigns a unique UID, two identical UID signatures can share data.
The negative effects of Multithreading: 1, static members and Singleton mode complete failure (through memory to share data will fail), 2, the thread synchronization mechanism is completely invalid, 3, sharedpreferences reliability decreased (only one sharedpreferences in memory); 4. Application will be created multiple times; conclusion: in multi-process mode, the components of different processes will have independent virtual machine, application and memory space.
IPC BasicsSerializable:java provides a serialization interface that uses classes to implement Serializable and declare inside the interface
 
   
  
  1. private static final long serialVersionUID = 51989342343L;
The serialization and deserialization of objects uses ObjectOutputStream and ObjectInputStream. Manually specifying SERIALVERSIONUID can largely avoid deserializationfailed.
Parcelable:
Android provides the recommended new serialization interface for high efficiency.
  
 
  1. public class User implements Parcelable, Serializable {
  2. private static final long serialVersionUID = 519067123721295773L;
  3. public int userId;
  4. public String userName;
  5. public boolean isMale;
  6. public Book book;
  7. public User() {
  8. }
  9. public User(int userId, String userName, boolean isMale) {
  10. this.userId = userId;
  11. this.userName = userName;
  12. this.isMale = isMale;
  13. }
  14. Returns the content description of the current object
  15. public int describeContents() {
  16. return 0;
  17. }

  18. Serialization of
  19. public void writeToParcel(Parcel out, int flags) {
  20. out.writeInt(userId);
  21. out.writeString(userName);
  22. out.writeInt(isMale ? 1 : 0);
  23. out.writeParcelable(book, 0);
  24. }
  25. public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
  26. Create the original object from the serialized object
  27. public User createFromParcel(Parcel in) {
  28. return new User(in);
  29. }
  30. Creates an array of original objects of the specified length
  31. public User[] newArray(int size) {
  32. return new User[size];
  33. }
  34. };

  35. Deserialization
  36. private User(Parcel in) {
  37. userId = in.readInt();
  38. userName = in.readString();
  39. isMale = in.readInt() == 1;
  40. book = in. readparcelable (Thread. CurrentThread (). Getcontextclassloader ());
  41. }
  42. @Override
  43. public String toString() {
  44. return String.format(
  45. "User:{userId:%s, userName:%s, isMale:%s}, with child:{%s}",
  46. userId, userName, isMale, book);
  47. }
  48. }

BINDER:IPC angle: A cross-process communication mode in Android; Android Framework angle: ServiceManager connect various manager and corresponding ManagerService bridge Android Application layer: The medium of communication between client and server, when Bindservice, the server returns a Binder object containing the service-side business call, through which the client can obtain the server Data and services (including General Service and AIDL based services).
Implementation of the IBinder interface, in the Android development is mainly used in the service, including Aidl and Messenger, common servece binder does not involve interprocess communication, the bottom of the messenger is aidl. The essence of Aidl is that the system provides us with a tool to implement binder quickly. Two important methods of binder are Linktodeath and Unlinktodeath, which are notified when Binder dies.

How the IPC is implemented
Intent additional extra (pass bundle data), ContentProvider, file SD card sharing (suitable for communication between processes requiring low data synchronization), Binder, network communication (Socket), Aidl
Messenger: Data transfer must be put into message, Messenger and message are implemented Parcelable interface, serial processing.            Aidl:android uses an Interface definition language (Interface definition Language,idl) to expose the interface of the service.           Aidl is used extensively in parcelable to serialize objects.           If you use a custom Parcelable object in Aidl, you must create a aidl file with the same name. Aidl process: Create a service and a aidl interface, and then create a class that inherits from the stub class in the Aidl interface and implements the abstract method in the stub, returning the object of this class in the service's Onbind method, The client can then bind the Server service and establish a connection to access the remote server.
Socket: Not only can realize interprocess communication, but also can realize communication between devices, if the IP between devices is visible; ContentProvider: inherently suitable for interprocess communication, bottom binder


Binder Connection PoolBinderpool: The binder request for each business module is forwarded uniformly to the remote service for execution, greatly improving the development efficiency of the aidl and avoiding the creation of a large number of service.





From for notes (Wiz)

2nd Chapter IPC Mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.