1,bundle one of the simplest ways to pass data between processes
Use bundles to pass data between processes (the data is serialized and the basic data types are serialized).
intent Intent = new Intent ( "Romote" ) Bundle Bundle=new Bundle () Bundle.putparcelable (key,new book (1 , Span class= "hljs-string" > "WCX" ) Intent.putextras (bundle) ; StartActivity (Intent) Another process gets the data Book book=getintent () .getparcelableextra (Mainactivity) Log.e (Log_tag,book.tostring ()) Span class= "hljs-comment";
2, by file sharing way
For example, serializing an object to a hard disk, deserializing it in another process, and getting the object's data.
Data sharing can also be achieved through a process written to a file by the B process and by the way it reads the file.
However, if you read and write multiple processes concurrently, it can cause a lot of trouble. So there are concurrent read and write operations that require thread synchronization.
It is suitable for communication between processes that are not high on data synchronization requirements.
3,messenger
Messenger can pass a message object in a different process. The message can be placed in the data we need to pass.
It is a lightweight IPC approach, and its bottom layer is aidl.
Steps to implement:
In a remote service, the Messenger object is created with a handler binding. This handler is responsible for handling messages sent by the client.
Returns the Binder object in Messenger by Getbinder () in the Bind method.
On the client, after the service is connected through the binding service, gets to the binder object, creating Messenger through the Binder object.
Encapsulates the message that the client needs to send.
Obj of the message can pass data in the same process, but in a multi-process, obj can only specify the system-provided object that implements the Parceable interface. So the basic data type (which is implemented by the Serializable interface) is not possible. The custom implementation of the Parceable interface object is also not possible. But fortunately, you can use Bundle,bundle to support all serialized objects.
When the encapsulated data is complete, send a message via Messenger.
The handler of the remote service is then processed to handle the data that is passed over.
Remote service:
Private Static FinalString Log_tag ="====>";@Nullable @Override PublicIBinderOnbind(Intent Intent) {LOG.E (Log_tag,"Thread ID:"+ android.os.Process.myPid () +"");returnMessenger.getbinder (); }//handler processing messages sent by clients PrivateHandler handler=NewHandler () {@Override Public void Handlemessage(Message msg) {Super. Handlemessage (msg);Switch(Msg.what) { CaseMainActivity.CORRECT:String name=msg.getdata (). GetString ("Football"); LOG.E (Log_tag,name); Break;default: LOG.E (Log_tag,"Default"); Break; } } }; Messenger messenger=NewMessenger (handler);
Client:
Private void Messengermethod(){//Binding ServicesIntent intent=NewIntent (); Intent.setcomponent (NewComponentName ("Asule.myhope","Asule.myhope.MyMessengerService")); Bindservice (intent,conn,bind_auto_create); }PrivateServiceconnection conn=NewServiceconnection () {@Override Public void onserviceconnected(componentname name, IBinder service) {Messenger =NewMessenger (service); Message message=NewMessage (); Message.what=correct; Bundle bundle=NewBundle (); Bundle.putstring ("Football","Ronaldo"); Message.setdata (bundle);Try{messenger.send (message); }Catch(RemoteException e) {E.printstacktrace (); } }@Override Public void onservicedisconnected(componentname name) { } };
The above example shows that the client sends the data to the server. Remote services can also reply to client data via Messenger.
After the client sends the data, the client's messager is passed to the server via the Message.replayto parameter. The server uses the Messager to send data to the client.
At this point the client only needs to create messenger and bind to a handler that processes the data.
The IPC mode of Android