How to Implement the binder client program

Source: Internet
Author: User

 

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?
  1. $ CD framework/base/
  2. $ Mkdir exampleclient
  3. $ 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?
  1. // File: example. h
  2. # Ifndef android_byn_example_h
  3. # Define android_byn_example_h
  4. Namespace android
  5. {
  6. Class Example {
  7. Public:
  8. Void add100 (int n );
  9. PRIVATE:
  10. Static const void getexampleservice ();
  11. };
  12. }; // Namespace
  13. # 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?
  1. // File: example. cpp
  2. # Include <binder/iservicemanager. h>
  3. # Include <binder/ipcthreadstate. h>
  4. # Include "example. H"
  5. Namespace android
  6. {
  7. Sp <ibinder> binder;
  8. Void example: add100 (int n)
  9. {
  10. Getexampleservice ();
  11. Parcel data, reply;
  12. Int answer;
  13. Data. writeint32 (getpid ());
  14. Data. writeint32 (N );
  15. LogE ("bpexampleservice: Create remote ()-> transact ()/n ");
  16. Binder-> transact (0, Data, & reply );
  17. Answer = reply. readint32 ();
  18. Printf ("answner = % d/N", answer );
  19. Return;
  20. }
  21. Const void example: getexampleservice ()
  22. {
  23. Sp <iservicemanager> Sm = defaultservicemanager ();
  24. Binder = Sm-> getservice (string16 ("Byn. Example "));
  25. LogE ("Example: getexampleservice % P/N", Sm. Get ());
  26. If (Binder = 0 ){
  27. Logw ("exampleservice not published, waiting ...");
  28. Return;
  29. }
  30. }
  31. }; // Namespace
  32. Using namespace android;
  33. Int main (INT argc, char ** argv)
  34. {
  35. Example * P = new example ();
  36. P-> add100 (1 );
  37. Return 0;
  38. }

// 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?
  1. # File: Example
  2. Local_path: = $ (call my-DIR)
  3. Include $ (clear_vars)
  4. Local_src_files: = \
  5. Example. cpp
  6. Local_c_includes: = $ (jni_h_include)
  7. Local_shared_libraries: = \
  8. Libutils libbinder libexample
  9. Local_module_tags: = optional
  10. Local_prelink_module: = false
  11. Local_module: = example
  12. 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?
  1. $ Cd ~ /Working_directory/
  2. $ 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?
  1. $ Make Snod

$ Make Snod

Step 2:

Start the simulator and use ADB to connect

View plaincopy to clipboardprint?
  1. $ Emulator &

$ Emulator & <br/>Connect with ADB

View plaincopy to clipboardprint?
  1. $ ADB Shell

$ ADB Shell

Step 2

In the simulator, view the libexample. So, exampleserver, and example files.

View plaincopy to clipboardprint?
  1. # Ls system/lib/libex *
  2. # Ls system/bin/examp

# Ls system/lib/libex * <br/> # ls system/bin/exampAs shown in:

Step 2: Start exampleserver

View plaincopy to clipboardprint?
  1. #./System/bin/exampleserver &

#./System/bin/exampleserver & <br/>

Step 2: Start the client program example

View plaincopy to clipboardprint?
  1. #./System/bin/Example

#./System/bin/example <br/>As shown in:

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.