Add the underlying core services for the Android

2010-09-10  来源:本站原创  分类:Mobile  人气:232 

Add the underlying core services for the Android
1. Why write the underlying core services?

Because the underlying framework of the core service is the closest Android Linux / Driver part. In order to give full play to the difference of hardware features, the core service is to allow Java applications to use the upper Driver / HW Device important channel characteristics. For example, Media, Telephone and so the underlying hardware.

In the boot process, you can start the core services (Chinese character input method services, etc.), so many applications to common use.

As the share, so Java applications can effectively reduce the size (Size).
2. The core services and Java Service Layer What is the difference and relationship?

Android has a two-tier service

- Java layer SDK-based Service

- C + + layer Code Service

3. Write your own core services (C + + layer)
1). Points

Core services are usually in a separate process (Process) in the implementation.

Must provide IBinder interface, so that the application process can be cross-binding (Binding) and call.

The sharing, it must ensure that the multi-thread-safe (Thread-safe).

Using C + + to achieve, and call IServiceManager:: addService () function added to the system of Binder Driver in.

Upper application for the service through the ServiceManager.

Upper application by IBinder:: transact () function to interact with the core data services.
2). Add Service

The following details how to add a low-level service to the system, assuming service called AddService, its purpose is to add 1,000 incoming parameter and returns the results.
Service implementation

Into the android source directory frameworks / base, in the directory to build their own directory, say addservice, then in this directory create two subdirectories addserver and libaddservice, addserver startup files for storage services, with the ultimate raw as can be implementation of the file, when the system starts running, libaddservice for the implementation of file storage services will eventually generate dynamic link library, a addserver call.

First, the service implementation file includes two files, AddService.h and AddService.cpp,

The following is AddService.h:



# Include <utils/RefBase.h>

# Include <binder/IInterface.h>

# Include <binder/Parcel.h>

# Include <utils/threads.h>

namespace android (

class AddService: public BBinder (/ / derived from BBinder achieve local interface


static int instantiate ();

AddService ();

virtual ~ AddService ();

virtual status_t onTransact (uint32_t, const Parcel &, Parcel *, uint32_t);


); / / Namespace

# Endif

Then the service implementation file AddService.cpp:

# Include "AddService.h"

# Include <binder/IServiceManager.h>

# Include <binder/IPCThreadState.h>

namespace android (

static struct sigaction oldact;

static pthread_key_t sigbuskey;

/ / Register themselves to the system

int AddService:: instantiate () (

LOGE ("AddService instantiate");

int r = defaultServiceManager () -> addService (

String16 ("guilh.add"), new AddService ());// key here is to / / AddSerice this service to the service, called guilh.add Binder Driver

LOGE ("AddService r =% d \ n", r);

return r;


/ / Constructor

AddService:: AddService ()


LOGV ("AddService created");

mNextConnId = 1;

pthread_key_create (& sigbuskey, NULL);


/ / Destructor

AddService:: ~ AddService ()


pthread_key_delete (sigbuskey);

LOGV ("AddService destroyed");


/ / This is the local implementation of specific services, functionalities have to be placed on the inside, by passing the code (code) / / different to perform different operations, the upper hint for different api.

status_t AddService:: onTransact (

uint32_t code, const Parcel & data, Parcel * reply, uint32_t flags) (

switch (code) (

case 0: (/ / according to the different code to perform different operations

pid_t pid = data.readInt32 ();

int num = data.readInt32 ();

num = num + 1000;

reply-> writeInt32 (num);

return NO_ERROR;




return BBinder:: onTransact (code, data, reply, flags);


)); / / Namespace

The following is a compilation of services, and above cpp together.

LOCAL_PATH: = $ (call my-dir)

include $ (CLEAR_VARS)






libcutils \

libutils \

libbinder \




include $ (BUILD_SHARED_LIBRARY) # this line that compiled as dynamic libraries

Exit the command line to the android / directory level load build environment. Build /

Then lunch.

Then cd / android / frameworks / base / addservice / libaddservice / directory input mm

After the out directory output file.

Completed the first step in this core service.

Service process to achieve

Access to cd / android / frameworks / base / addservice / addserver / directory

Add a file addserver.cpp, file reads as follows:

# Include <sys/types.h>

# Include <unistd.h>

# Include <grp.h>

# Include <binder/IPCThreadState.h>

# Include <binder/ProcessState.h>

# Include <binder/IServiceManager.h>

# Include <utils/Log.h>

# Include <private/android_filesystem_config.h>

# Include ".. / libaddservice / AddService.h"

/ / # Include <libadd/AddService.h>

using namespace android;

int main (int argc, char ** argv)


sp <ProcessState> proc (ProcessState:: self ());

sp <IServiceManager> sm = defaultServiceManager ();// get ServiceManager

LOGI ("ServiceManager:% p", sm.get ());

AddService:: instantiate ();// in itself to the ServiceManager

ProcessState:: self () -> startThreadPool ();// start the buffer pool

IPCThreadState:: self () -> joinThreadPool ();// here is to service to the closed loop process Binder


Above the standard operation for the underlying services.

Here is the service makefile:

include $ (CLEAR_VARS)




libAdd \

libutils \


LOCAL_MODULE: = addserver

include $ (BUILD_EXECUTABLE) / / compiled into an executable file

After the implementation of mm out of the current directory can be in the out directory system / bin executable files under the output addserver.
Achieve service process to start automatically

Access to / android / system / core / rootdir / directory there init.rc file

vi init.rc

Add in the service

service addservice / system / bin / addserver / / to / system / bin / addserver start as a service, the service name for the addservice (this is not important).

Last Exit to android / directory to perform a full compilation:

Input. Build /




Open Simulator Emulator

Open another shell terminal, type adb shell into the simulator mode if adbshell system did not find the command prompt in android/out/host/linux-x86/bin / type. / Adb shell

Ps see the input process to find if there addserver process

If you have half the success.

Test our service

Random in the android / packages / apps in a simple application

Here you can directly create a good project in eclipse to copy android / packages / apps, and then add a file for the application, you can copy from other applications to modify.

Testing services in the application code:

private void test () (

try (

IBinder binder = ServiceManager.getService ("guilh.add ");// access to services

Parcel data = Parcel.obtain ();

Parcel reply = Parcel.obtain ();

if (binder == null)

Log.d (TAG, "failed to get service");

data.writeInt (Process.myPid ());// fixed operation

data.writeInt (100); / / pass parameters

binder.transact (0, data, reply, 0); / / implementation of the remote call

Log.d (TAG, "result =" + reply.readInt ());// test results

) Catch (Exception e) (

Log.d (TAG, e.toString ());


(The above information comes from some of the core services of high Huan Tong android welcome your comments and suggestions)

  • Add the underlying core services for the Android 2010-09-10

    Add the underlying core services for the Android 1. Why write the underlying core services? Because the underlying framework of the core service is the closest Android Linux / Driver part. In order to give full play to the difference of hardware feat

  • Services for the Android add a Java layer 2010-09-10

    Services for the Android add a Java layer Similar with the previous article, this article aims to briefly explain how to add your own java level of service, related theory has many articles online, not detailed in this. Java layer services shall be f

  • Add progress bar to solve the abnormal android 2010-08-04

    Today to add a progress bar in, that is, the thread with a new communication fills to log, for example, the code is as follows: private ProgressDialog progressDialog = null; private Handler mHandler = new Handler () ( @ Override public void handleMes

  • After reinstalling the system to add mysql and apache services 2011-03-16

    One. Add mysql service 1. Installation Services - Run cmd - Enter the directory install mysql - Into the bin directory Run mysqld-install Then, in cmd, enter mysql-u root-p find prompt "mysql is not an internal command or external command"; need

  • Some of the underlying basics android (Reprinted) 2010-05-31

    An important description of the package: to provide high-level programming model, provides basic runtime environment android.content: contains various data on the device to access and release of the class android.database: Browse by cont

  • The status bar at the android (statusbar) to add menu, home and way back shortcut 2010-11-15

    Completely changed the status bar, recommend a few png images, add Frameworks / base / core / res / res / drawable Next. Best to do a background image, replace statusbar_background.png In addition, I added a few icon, is the home menu, and back were

  • The status bar at the android (statusbar) to add menu, home and back shortcuts Improvement 2010-11-19 Should be noted that: just a few days to start android, not too familiar with the system, this article is based on a Zhuantie done before. Just think of his method some trouble, but the eff

  • List of Android system services 2011-01-14

    System_Server process runs in the service system server process more, which is the basis of the framework of the android Native Services SurfaceFlinger This is the framebuffer synthesis service to each application and the logic of the application win

  • android system services 2010-09-26

    Constant Values String ADD_SHORTCUT_ACTION action: in the system to add a shortcut. . "Android.intent.action.ADD_SHORTCUT" String ALL_APPS_ACTION action: list of all available applications. Input: None. "Android.intent.action.ALL_APPS"

  • Android Technology series of the seventh - volume services 2011-01-13

    Android's volume services are primarily used to manage usb / sd cards and other external storage devices. Platform can operate on the external storage device and polling the state, when the external storage device state changes, volume services will

  • To add or remove services linux boot process 2010-12-06

    In the Linux system, a Services, start, stop and restart usually through / etc / init.d directory script to control. However, start or change the run level, is in the / etc / rcX.d to search for in the script. Where X is the runlevel number. This art

  • For the linux boot process to add or remove services 2010-12-06

    In the Linux system, a Services, start, stop and restart usually is / etc / init.d directory script to control. However, start or change run levels, in the / etc / rcX.d to search for in the script. Where X is the runlevel number. This article will e

  • In-depth Android [4] - component called 2010-04-29

    Original Address: Intent Analysis Component-based architecture system, in addition to well-defined components, how to assemble these components together, and an art. In Android in, Inte

  • Adroid core analysis 2010-06-27

    On the blog: 2010 In June. 14 Day Android core analysis (28 )----- Android GDI of Surface & Canvas Surface & Canvas Canvas is the meaning of the canvas. Android's mapping upper Canvas instance by almost complete, in fa

  • Documents on the Chinese android 2010-07-08

    What is Android? Android is a specialized set of software for mobile devices, which includes an operating system, middleware and some important applications. Beta version of the Android SDK provides the Android platform using the Java language for An

  • android in the work flow Sensor 2010-12-30

    JAVA program as long as we use the sensor interface to the general look SensorListener up like this ************************************************** ************ ApiDemo: mGraphView = new GraphView (this); mSensorManager.registerListener (mGraphVie

  • Android event handling of the event handler + Android + Android processes input events 2011-01-14 Key events For key events, call mDevices-> layoutMap-> map to map. The actual mapping is KeyLayoutMap:: map completed, K eyLayoutMap class reads the configuration file qwerty.kl, determine

  • [zz] android introduction 2010-03-29

    Android applications in conjunction with a core application package for release, the application package includes email client, SMS short messaging programs, calendar, maps, browser, contact management programs. All applications are written using JAVA. An

  • In-depth Android [3] - Component entry 2010-04-29

    Android Components Original Address: Cross as many different angles, near and far different level. - "Title Xilin Wall" Component (Component), referring to the so-called time struc

  • Android system, init starts the process of collection of 2010-06-23

    Android init starts the process of Analysis android startup process, from the core, we should first start from the init file system, because init is the kernel into the file system after the first run of the program, often we can specify linux kernel