GithubHelp home page GithubHelp logo

chenguangshen / android-helloworldservice Goto Github PK

View Code? Open in Web Editor NEW

This project forked from keesj/android-helloworldservice

0.0 2.0 0.0 19.33 MB

This is a skeleton application that creates an Android service in C++, accessing it via Java/Binder.

android-helloworldservice's Introduction

Dummy native binder android HelloWorld sample created to better understand
the possibilities of creating pure native android services. The current
status is that it certainly is possible to create native binder services
under Android

Scope:
We are currently mostly interested in creating a native service started from
init.rc and we would like this service to be pure native (no JVM). We want
this service to act as much as possible like any other android service. We
would also like it to be possible for the client of the service to not depend
on Android internal API's.

Limitations:
The native service currently runs as system user and uses the
ServiceManager.addService('name',binder) method. This method is not available
to normal packages installed on a stock phone. For that limitation to be
worked around it it probably is needed to let the android system (ActivityManager?)
let create the IBinder object and pass it to the native


On the following thread Dianne Hackborn states
https://groups.google.com/d/topic/android-porting/yK17XZXkupI/discussion

 The Java runtime has wrappers around C++ IBinder.  This is what Java's Binder is.
 You can write a JNI function that returns a Java Binder and in its implementation
 instantiate a C++ IBinder interface and return it.  You will need to use this magic
 function in libandroid_runtime to do this:

  extern jobject javaObjectForIBinder(JNIEnv* env, const sp<IBinder>& val);
 The header for this is currently in core/jni/android_util_Binder.h.

It will be probably nececary to pass a Binder object created to the native code.
An other problem might be related to the threading

This code was taken from http://github.com/mcr/Android-HelloWorldService and
modified to work on gingerbread.

Reading the code:

Best is to start reading  libhelloworldservice/include/IHelloWorld.h comparing it to the code generated by aidl
and follow with native implementations of that interface
libhelloworldservice/src/helloworldservice.cpp
libhelloworldservice/src/helloworldclient.cpp

After that read the native "server" code helloworld/main_helloworldservice.cpp to see how the native code works

to Find the usage read AndroidManifest.xml src/org/credil/helloworldservice/HelloWorldActivity.java and star fix code in
src/org/credil/helloworldservice/HelloWorldService.java :P


Building/playing around:

For information about developing "inside" Android
https://github.com/keesj/gomo/wiki/AndroidPlatformDevelopmentWorkflow

Checkout this this branch into your android platform tree
. build/envsetup.sh
lunch sdk-eng
make


After that 
and goto the Android-HelloWorldService directory
use mm/adb sync to build deploy

Running:

From the shell:
# /system/bin/helloworldservice
hello: fun
hello: Using interfaces
hello: Calling from Java now

# /system/bin/helloworldclient

To launch the Activity use the activity launcher.


Native binder:
/* Interfaces extend */
class IInterface extends RefBase
    + asBinder()
    + onAsBinder();

/* The native class implementation extends BnInterface
 * this is the instance that will be given to the service
 * manager
 *
 * BnInterface is the base implementation for a native (local) IInterface
 **/
class BnInterface<INTERFACE> extends BBinder
    + queryLocalInterface(descriptor)

/**
 * The BBinder class is the standard implementation of IBinder for a IInterface object
 **/
class BBinder extends IBinder
    + getInterfaceDescriptor()
    + attachObject(id ...) /* housekeeping of clients? */
    + findObject(id ...)
    + detachObject(id ...);

/* remote stub implemntation extends */
class BpInterface<INTERFACE> extends BpRefBase
    + onAsBinder();



/* BpRefBase defines a ref counted class having a method to get
 * a binder object
 */
class BpRefBase extends RefBase
    + remote() IBinder*

android-helloworldservice's People

Contributors

keesj avatar

Watchers

James Cloos avatar Chenguang Shen 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.