The way of communication in Android process


     This article is from http://blog.csdn.net/liuxian13183/  , and the reference must be indicated!

in the big direction

First, use the method of process sharing, often using android:process=remote, so that a new process is opened, so that all processes can access this process, so that services can be shared among multiple processes; and android:process=:remote is equivalent to Give the current process a private process to maintain its own business processing. Starting a new process can be used in components such as activity, service, broadcastReceiver, and ContentProvider.

If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the service runs in that process.  If the process name begins with a lowercase character, the service will run in a global process of that name, provided that it has permission to do so. This allows components in different applications to share a process, reducing resource usage.

Second, using the broadcast method, you can set category, component, package to distinguish broadcast, and use custom permissions to limit

Method 1. Use AIDL to do it

Third, use Message+Binder

Fourth, use Socket

Fifth, shared memory, such as ContentProvider

Linux has pipeline (Pipe), signal (Signal), message (Message), jump (Trace), followed by semaphore (Semaphore), ShareMemory

The reason why Binder can support cross-process communication is that it implements the IBinder interface, and the system defines that implementing this interface means giving the process the function of communication.

Advantages: only one time to copy data, then both Pipe and Socket need two times; secondly, the security is high, and it will not expose the address like Socket and be replaced by others;

Communication mechanism: Service registers with ServiceManager to obtain virtual Uid and Pid; Client requests ServiceManager to obtain virtual Uid and Pid, ​​as well as the Proxy of the target object. The bottom layer is transmitted through hardware protocol and uses Binder for communication.

When communicating, the Client holds the Proxy, and the ServiceManger converts it and calls it to the Service. The three run in three separate processes. Client/Sever full duplex, Sever/Client for each other

Get whether the process is in the foreground:

    public static boolean isRunningForeground(Application application) {
        ActivityManager activityManager = (ActivityManager) application.getSystemService("activity");
        List appProcessInfos = activityManager.getRunningAppProcesses();
        Iterator var3 = appProcessInfos.iterator();
        ActivityManager.RunningAppProcessInfo appProcessInfo;
        do {
            if (!var3.hasNext()) {
                return false;
            }
            appProcessInfo = (ActivityManager.RunningAppProcessInfo) var3.next();
        }
        while (appProcessInfo.importance != 100 || !appProcessInfo.processName.equals(application.getApplicationInfo().processName));
        return true;
    }


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325985805&siteId=291194637