References:
Http://blog.csdn.net/baiyanning/article/details/6191682
The previous chapter describes how to binder the server.
The next step is how to implement the client program. The client executable program we will introduce is example
Step 2:
Create an exampleclient directory under the framework/base directory to save the client source code:
View plaincopy to clipboardprint?
- $ CD framework/base/
- $ Mkdir exampleclient
- $ CD exampleclient/
$ CD framework/base/<br/> $ mkdir exampleclient <br/> $ CD exampleclient/
Step 2:
The exampleclient directory contains three source files: example. H, example. cpp, and Android. mk.
The content of example. H is as follows:
View plaincopy to clipboardprint?
- // File: example. h
- # Ifndef android_byn_example_h
- # Define android_byn_example_h
- Namespace android
- {
- Class Example {
- Public:
- Void add100 (int n );
- PRIVATE:
- Static const void getexampleservice ();
- };
- }; // Namespace
- # Endif // android_byn_example_h
// File: Example. h <br/> # ifndef android_byn_example_h <br/> # define android_byn_example_h </P> <p> namespace Android <br/>{< br/> class example {<br/> public: <br/> void add100 (int n); <br/> PRIVATE: <br/> static const void getexampleservice (); <br/>}; <br/> }; // namespace <br/> # endif // android_byn_example_h <br/>The content of example. cpp is as follows:
View plaincopy to clipboardprint?
- // File: example. cpp
- # Include <binder/iservicemanager. h>
- # Include <binder/ipcthreadstate. h>
- # Include "example. H"
- Namespace android
- {
- Sp <ibinder> binder;
- Void example: add100 (int n)
- {
- Getexampleservice ();
- Parcel data, reply;
- Int answer;
- Data. writeint32 (getpid ());
- Data. writeint32 (N );
- LogE ("bpexampleservice: Create remote ()-> transact ()/n ");
- Binder-> transact (0, Data, & reply );
- Answer = reply. readint32 ();
- Printf ("answner = % d/N", answer );
- Return;
- }
- Const void example: getexampleservice ()
- {
- Sp <iservicemanager> Sm = defaultservicemanager ();
- Binder = Sm-> getservice (string16 ("Byn. Example "));
- LogE ("Example: getexampleservice % P/N", Sm. Get ());
- If (Binder = 0 ){
- Logw ("exampleservice not published, waiting ...");
- Return;
- }
- }
- }; // Namespace
- Using namespace android;
- Int main (INT argc, char ** argv)
- {
- Example * P = new example ();
- P-> add100 (1 );
- Return 0;
- }
// File: Example. CPP <br/> # include <binder/iservicemanager. h> <br/> # include <binder/ipcthreadstate. h> <br/> # include "example. H "</P> <p> namespace Android <br/> {<br/> sp <ibinder> binder; <br/> void example: add100 (int n) <br/>{< br/> getexampleservice (); <br/> parcel data, reply; <br/> int answer; </P> <p> data. writeint32 (getpid (); <br/> data. writeint32 (n); <br/> LogE ("bpexampleservice: Create remote ()-> transact ()/n"); <br/> binder-> transact (0, data, & reply); <br/> answer = reply. readint32 (); <br/> printf ("answner = % d/N", answer); <br/> return; <br/>}</P> <p> const void example: getexampleservice () <br/>{< br/> sp <iservicemanager> Sm = defaservicservicemanager (); <br/> binder = Sm-> getservice (string16 ("Byn. example "); <br/> LogE (" Example: getexampleservice % P/N ", SM. get (); <br/> If (Binder = 0) {<br/> logw ("exampleservice not published, waiting... "); <br/> return; <br/>}< br/>}; // namespace </P> <p> using namespace android; </P> <p> int main (INT argc, char ** argv) <br/> {<br/> example * P = new example (); <br/> P-> add100 (1); <br/> return 0; <br/>}< br/>The content of the android. mk file is as follows:
View plaincopy to clipboardprint?
- # File: Example
- Local_path: = $ (call my-DIR)
- Include $ (clear_vars)
- Local_src_files: = \
- Example. cpp
- Local_c_includes: = $ (jni_h_include)
- Local_shared_libraries: = \
- Libutils libbinder libexample
- Local_module_tags: = optional
- Local_prelink_module: = false
- Local_module: = example
- Include $ (build_executable)
# File: Example <br/> local_path: = $ (call my-DIR) <br/> include $ (clear_vars) <br/> local_src_files: ==< br/> example. CPP <br/> local_c_includes :=$ (jni_h_include) <br/> local_shared_libraries :=\< br/> libutils libbinder libexample <br/> local_module_tags: = optional <br/> local_prelink_module: = false <br/> local_module: = example </P> <p> include $ (build_executable) <br/>
Step 2:
Compile the example executable program:
View plaincopy to clipboardprint?
- $ Cd ~ /Working_directory/
- $ Mmm framework/base/exampleclient
$ Cd ~ /Working_directory/<br/> $ Mmm framework/base/exampleclientFor example:
After compilation, you can see the executable program example in the out/target/product/generic/system/bin/directory.
In this way, the client program is generated, and the next step is to verify our program.
Step 2:
Make in the root directory of the android source code to generate the system. imgfile
View plaincopy to clipboardprint?
- $ Make Snod
$ Make Snod
Step 2:
Start the simulator and use ADB to connect
View plaincopy to clipboardprint?
- $ Emulator &
$ Emulator & <br/>Connect with ADB
View plaincopy to clipboardprint?
- $ ADB Shell
$ ADB Shell
Step 2
In the simulator, view the libexample. So, exampleserver, and example files.
View plaincopy to clipboardprint?
- # Ls system/lib/libex *
- # Ls system/bin/examp
# Ls system/lib/libex * <br/> # ls system/bin/exampAs shown in:
Step 2: Start exampleserver
View plaincopy to clipboardprint?
- #./System/bin/exampleserver &
#./System/bin/exampleserver & <br/>
Step 2: Start the client program example
View plaincopy to clipboardprint?
- #./System/bin/Example
#./System/bin/example <br/>As shown in: