Six ways of IPC in Android
1. Use Bundle
- The simplest way of inter-process communication: Intent + Bundle;
- Support three components: Activity, Service, BroadcastReceiver;
- Implements the Parcelable interface;
- Can transfer basic data types, objects that implement Parcelable interface, objects that implement Serizlizable interface, and some special objects supported by Android;
Special scenario : To transfer data from process A to process B, you can start the Service component (such as IntentService) in B through Intent+Bundle from A, and let the Service perform calculations in the background. After the calculation is completed, directly start the components in B to be started. , so that the target component can get the result directly from B. See the picture below for details:
2. Use file sharing
- There is no specific requirement for the file format, for example, you can try a text file or an XML file, as long as the read/write parties agree on the data format;
- There will be problems with multi-threaded operations, so the file sharing method is suitable for inter-process communication that does not require high file data synchronization, and the problem of concurrent read/write should be properly handled;
- SharedPreferences is a special case, which is a lightweight storage solution provided by Android: