Code Monkey home page Code Monkey logo

binderdemo's Introduction

This is a bare bones example of how to use Android IPC binders from native C++ code. For some high-level explanations see the following two blog posts:

Quick overview on how to build the code:

  • This code depends on the parts of Android that are not included in the NDK, so you'll need to repo sync and build a complete image to have all the dependencies in place. For details, see:
  • cd <RepoTop>
  • source build/envsetup.sh
  • lunch
  • cd <RepoTop>/external
  • git clone https://github.com/gburca/BinderDemo.git
  • cd BinderDemo
  • mm
  • The binary should be in <RepoTop>/out/target/product/*/system/bin/binder. Copy it to /system/bin/ on the phone.

Test the binary:

  • adb logcat -v time binder_demo:* *:S
  • adb shell binder
  • adb shell binder 456

The adb logcat -v time binder_demo:* *:S should show:

01-04 12:36:01.453 D/binder_demo( 7575): We're the service
01-04 12:36:01.454 D/binder_demo( 7575): IDemo::IDemo()
01-04 12:36:01.457 D/binder_demo( 7575): Demo service is now ready
01-04 12:36:07.658 D/binder_demo( 7577): We're the client: 456
01-04 12:36:07.660 D/binder_demo( 7577): IDemo::IDemo()
01-04 12:36:07.660 D/binder_demo( 7577): BpDemo::BpDemo()
01-04 12:36:07.660 D/binder_demo( 7577): BpDemo::alert()
01-04 12:36:07.661 D/binder_demo( 7575): BnDemo::onTransact(1) 17
01-04 12:36:07.661 D/binder_demo( 7575): Demo::alert()
01-04 12:36:07.661 D/binder_demo( 7575): BnDemo::onTransact(2) 16
01-04 12:36:07.661 D/binder_demo( 7575): BnDemo::onTransact got 456
01-04 12:36:07.661 D/binder_demo( 7575): Demo::push(456)
01-04 12:36:07.662 D/binder_demo( 7577): BpDemo::push(456)
01-04 12:36:07.662 D/binder_demo( 7575): BnDemo::onTransact(3) 16
01-04 12:36:07.662 D/binder_demo( 7575): Demo::add(456, 5)
01-04 12:36:07.662 D/binder_demo( 7575): BnDemo::onTransact add(456, 5) = 461
01-04 12:36:07.662 D/binder_demo( 7577): BpDemo::add transact reply
01-04 12:36:07.662 D/binder_demo( 7577): BpDemo::add(456, 5) = 461 (status: 0)
01-04 12:36:07.662 D/binder_demo( 7577): Addition result: 456 + 5 = 461
01-04 12:36:07.662 D/binder_demo( 7577): IDemo::~IDemo()

The adb shell binder should show:

Parcel(
  0x00000000: 00400000 00000004 00650044 006f006d '[email protected].'
  0x00000010: 00000000 00000010 00680054 00200065 '........T.h.e. .'
  0x00000020: 006c0061 00720065 00200074 00740073 'a.l.e.r.t. .s.t.'
  0x00000030: 00690072 0067006e 00000000          'r.i.n.g.....    ')
Demo::alert()
Parcel(
  0x00000000: 00400000 00000004 00650044 006f006d '[email protected].'
  0x00000010: 00000000 000001c8                   '........        ')
Demo::push(456)
Parcel(NULL)
Parcel(
  0x00000000: 00400000 00000004 00650044 006f006d '[email protected].'
  0x00000010: 00000000 000001c8 00000005          '............    ')
Demo::add(456, 5)
Parcel(NULL)

The adb shell binder 456 should show:

We're the client: 456
BpDemo::push parcel to be sent:
Parcel(
  0x00000000: 00400000 00000004 00650044 006f006d '[email protected].'
  0x00000010: 00000000 000001c8                   '........        ')
BpDemo::push parcel reply:
Parcel(NULL)
BpDemo::add parcel to be sent:
Parcel(
  0x00000000: 00400000 00000004 00650044 006f006d '[email protected].'
  0x00000010: 00000000 000001c8 00000005          '............    ')
Parcel(000001cd    '....')

The server added 5 to our 456 and returned 461 (0x01cd).

The code in this project is Copyright(C) by Gabriel Burca and released under the GPL (http://www.gnu.org/copyleft/gpl.html) license.

binderdemo's People

Contributors

gburca avatar eldarz avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.