Android interprocess communication is the best solution, built for simplicity
###Github
Introduce a,
Do Android developers often encounter the same company has multiple apps, and these apps need to communicate business. This is where the IPC problem needs to be solved, and ABridge easily solves the interprocess communication problem.
Ii. Android IPC mode
There are several common ways to communicate across processes: Bundles pass data through intents, file sharing, ContentProviders, Binder-based AIDL and Messenger, and sockets.
What is IPC?
Perhaps some partners are not very clear about the concept of IPC, here I briefly outline.
IPC stands for inter-process Communication or inter-process Communication. It refers to the Process of data exchange between two processes.
Thread is the smallest unit of CPU scheduling, and thread is a limited system resource. A process generally refers to a unit of execution, and on PC and mobile devices refers to a program or application. A process can contain multiple threads, so processes and threads are contained and contained. In the simplest case, a process can have only one thread, the main thread, also known as the UI thread in Android.
IPC is not unique to Android, and any operating system needs a corresponding IPC mechanism. For example, On Windows, interprocess communication can be carried out through clipboard. Android is a mobile operating system based on the Linux kernel. Its interprocess communication mode cannot be completely inherited from Linux. It has its own interprocess communication mode.
Four, according to ABridge
Before using ABridge, we could implement IPC through the above methods, but these methods were cumbersome and costly to learn. To that end, ABridge was born – a framework that can easily implement cross-process communication in a few lines of code.
ABridge provides two solutions for cross-processing to meet the business needs of different scenarios: one based on Messenger and the other based on AIDL. Of course, Messenger is essentially AIDL, but it’s wrapped so you don’t have to write an. AIDL file when you develop it.
Five, basic usage
- Solution 1: Based on Messenger
Step1 add dependencies
api "com.sjtu.yifei:abridge:xxx.xxx.xxx" Copy the code
Step2 initialization
public class MainApplication extends Application { @Override public void onCreate(a) { super.onCreate(); // Only one packagename can be used by multiple apps that need to communicate // Start the shared service using an APP as the server to communicate IBridge.init(this."packagename"); }}Copy the code
Step3 implement the interface MessengerReceiver for the activity that needs to communicate
public class TestMessengerActivity extends AppCompatActivity implements MessengerReceiver { // A sender for cross-process communication private MessengerSender sender; //MessengerReceiver Interface method sets the sender @Override public void setSender(MessengerSender sender) { this.sender = sender; } // The MessengerReceiver interface method accepts messages sent across processes @Override public void receiveMessage(Message message) { if (message.arg1 == ACTIVITYID) { // The client receives a message from the server String str = (String) message.getData().get("content"); tv_show_in_message.setText(str); }}}Copy the code
Step4 send messages across processes
/ / connected to public void sendMessage(String messageStr) Message message = Message.obtain(); message.arg1 = ACTIVITYID; Bundle bundle = new Bundle(); bundle.putString("content", messageStr); message.setData(bundle); sender.sendMessage(message); } Copy the code
- Scheme 2: Based on AIDL
Step1 add dependencies
api "com.sjtu.yifei:abridge:xxx.xxx.xxx" Copy the code
Step2 initialization
public class MainApplication extends Application { @Override public void onCreate(a) { super.onCreate(); // Only one packagename can be used by multiple apps that need to communicate // Start the shared service using an APP as the server to communicate IBridge.init(this."packagename"); }}Copy the code
Step3 implement the interface MessengerReceiver for the activity that needs to communicate
public class TestAIDLActivity extends AppCompatActivity implements IReceiver { // A sender for cross-process communication private ISender sender; // The IReceiver interface method sets the sender @Override public void setSender(ISender sender) { this.send = sender; } // The IReceiver interface method accepts messages sent across processes @Override public void receiveMessage(String message) { tv_user.setText(message); }}Copy the code
Step4 send messages across processes
/ / connected to public void sendMessage(String messageStr) send.sendMessage(messageStr); } Copy the code
Six, Email
[email protected] [email protected]