How to use sockets in Android to enable the implementation of the underlying and framework communications _android

Source: Internet
Author: User

The general native and framework communication is through JNI, but this is generally just a framework call native,native What if there is a message to inform the upper level? The Android GSP module provides a solution, but the implementation is somewhat complex, and here is a way to use socket communication to enable native and the framework to communicate freely, as follows:

The socket in Linux is encapsulated using JNI in Android. It is very convenient to use.

Since Android is linux based, Linux code executes before Java, so the general native end is the server. The framework end is the client.

Java Layer Main code:

View Sourceprint?
Localsocket s =null;
Localsocketaddress l;
s = new Localsocket ();
L = new localsocketaddress (socket_name,localsocketaddress.namespace.reserved);
S.connect (l);

At this point if the socket connection is not a problem, you can read and write like normal.

Native Layer main code:

View Sourceprint?
S_fdlisten = Android_get_control_socket (socket_name);
RET = Listen (S_fdlisten, n);
S_fdcommand = Accept (S_fdlisten, (SOCKADDR *) &peeraddr, &socklen);

If the connection is not a problem, you can use the Linux write/read to read and write the socket;

Here it is necessary to explain Socket_name, whose value is a string that is defined in init.rc. In other words, we can apply the socket resource we need by modifying the init.rc.

Here take Ril as an example to illustrate:

Service Ril-daemon/system/bin/rild 
socket rild Stream 660 root radio 
socket RILD-DEBUG stream 660 Radio System
   user root 
Group Radio cache inet Misc Audio

These are excerpts from the system\core\rootdir\init.rc in the Android 2.2 source code. As for its specific meaning can refer to init.c and system/core/init/readme.txt files. His role was to parse the init.rc by Init.c, and to start a daemon called Rild, an executable program that we can find in system/bin through the ADB shell rild files. The socket indicates that a socket resource is allocated for this daemon, which can be found under/dev/socket/rild. This article is the most critical place, the socket can not see the Guardian process can be very good up. The above socket_name is the string defined here (there is a string constant socket_name_ril in Ril.java and Ril.cpp, and his value is rild, corresponding to the above).

If we are going to customize a socket to communicate, we can add the following at the end of the init.rc

Service Myserver-daemon/system/bin/server 
Socket Server Stream 666 
oneshot

System/bin/server is where we compile the generated server program, in which we call

S_fdlisten = Android_get_control_socket ("server"); 
RET = Listen (S_fdlisten, n); 
S_fdcommand = Accept (S_fdlisten, (SOCKADDR *) &peeraddr, &socklen);

You can create a server-side program.

Java only needs to use the top of the code to communicate with the native, note that the Socket_name value must be up and down uniform and init.rc in the equality, here is "rild." The oneshot here must have, if not, your server will probably not get up.

The rest is only compiled.

About the compiler can refer to the RIL in the Android.mk and RILD.C and Ril.cpp, the head of their own file can be singled out.

First with the MM to compile their own modules, compiled, will be added to the module test, in the source root directory make Snod. Add the compiled output file to the system.img. Finally, System.img and randisk.img are copied to the corresponding platform of the SDK. Can. The main two IMG files are to be handcuffed, SYSTEM.IMG has your executable program, and Randisk.img has your init.rc. Userdata.img not sure.

At this point, you only need to write a client program in Java.

The above is a small set of Android to introduce the use of sockets so that the bottom and framework communication implementation method, to achieve a simulation of the background data login effect, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.