Add underlying core services for Android

Source: Internet
Author: User

 

This article from: http://blog.csdn.net/belyxiong/archive/2010/09/10/5875993.aspx

 

1. Why write the underlying core service?
Because the underlying core service is the closest part of the android framework to Linux/driver. To make full use of the differentiated features of hardware devices, core services are an important channel for upper-layer Java applications to use driver/HW device features. Such as media, telephone, and other underlying hardware.

During the boot process, you can start the core services (such as the Chinese Character Input Method), so that many applications can use them together.

Because it is shared, it can effectively reduce the size of Java applications ).

2. What are the differences and relationships between core services and Java-layer services?
Android has two-tier services

-- Java-layer SDK-based service

-- Code service at the C ++ Layer

 

 

3. Write your own core services (C ++ layer)
1). Key Points
The core service is usually executed in an independent process.

The ibinder interface must be provided to allow applications to bind and call cross-process data.

Because it is shared, multi-thread security (thread-safe) must be ensured ).

Use C ++ and call the iservicemanager: addservice () function to add it to the system's binder driver.

The upper-layer application obtains the service through servicemanager.

Upper-layer applications use the ibinder: transact () function to interact with core servers.

2). Add a service
The following describes how to add an underlying service to the system. Assume that the service is named addservice. The purpose is to add 1000 to the input parameter and return the result.

Service implementation
Go to the framework/base Directory of the android source code, and create your own directory under this directory. Assume It is addservice, and then create two subdirectories addserver and libaddservice in this directory, addserver is used to store the Service Startup file, which is generated as an executable file and run at system startup. libaddservice is used to store the service implementation file and will generate a dynamic link library, there is an addserver call.

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

Here is addservice. h:

# Ifndef android_guilh_add_service_h

# Define android_guilh_add_service_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 to implement local interfaces

Public:

Static int instantiate ();

Addservice ();

Virtual ~ Addservice ();

Virtual status_t ontransact (uint32_t, const parcel &, parcel *, uint32_t );

};

}; // Namespace

# Endif

The following is 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 yourself to the System

Int addservice: instantiate (){

LogE ("addservice instantiate ");

Int r = defaservicservicemanager ()-> addservice (

String16 ("guilh. Add"), new addservice (); // Add the // addserice service to the binder driver named guilh. Add

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 specific local implementation of the service. The function implementation should be put in this area, and different operations should be executed by passing in different code, the upper layer hides different APIs.

Status_t addservice: ontransact (

Uint32_t code, const parcel & Data, parcel * reply, uint32_t flags ){

Switch (CODE ){

Case 0: {// perform different operations based on different codes

Pid_t pid = data. readint32 ();

Int num = data. readint32 ();

Num = num+ 1000;

Reply-> writeint32 (Num );

Return no_error;

}

Break;

Default:

Return bbinder: ontransact (Code, Data, reply, flags );

}

}; // Namespace

The following is the Android. mk of the compilation service, which is put together with the CPP above.

Local_path: = $ (call my-DIR)

Include $ (clear_vars)

Local_src_files: =/

Addservice. cpp

Local_c_includes: =/

$ (Jni_h_include)

Local_shared_libraries: =/

Libcutils/

Libutils/

Libbinder/

Libandroid_runtime

Local_prelink_module: = false

Local_module: = libadd

Include $ (build_shared_library) # This line indicates compiling as a dynamic library

 

Log out of the command line and load the compiling environment at the android/directory level. Build/envsetup. Sh

Then lunch.

Then input mm in the CD/Android/frameworks/base/addservice/libaddservice/directory.

Then generate the libadd. So file in the out directory.

This completes the first step of core services.

Service Process Implementation
Go to the CD/Android/frameworks/base/addservice/addserver/directory.

Add an addserver. cpp file with the following content:

 

# 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 (); // Add yourself to servicemanager

Processstate: Self ()-> startthreadpool (); // start the buffer pool

Ipcthreadstate: Self ()-> jointhreadpool (); // Add the service to the binder Closed Loop process.

}

The above is the standard operation of the underlying service.

Here is the service makefile:

Include $ (clear_vars)

 

Local_src_files: =/

Addserver. cpp

 

Local_shared_libraries: =/

Libadd/

Libutils/

Libbinder

Local_module: = addserver

Include $ (build_executable) // compile as an executable file

After exiting, execute mm in the current directory to generate the addserver executable file under system/bin in the out directory.

Enable Automatic Running of service processes upon startup
There is an init. RC file in the/Android/system/CORE/rootdir/directory.

VI init. RC

Add

Service addservice/system/bin/addserver // start/system/bin/addserver as a service. The service name is addservice (this is not important ).

 

Finally, exit to the android/directory for full compilation:

Enter. Build/envsetup. Sh

Lunch

Make

After completion

Enable emulator

Open another shell terminal enter ADB shell enter simulator mode if adbshell system prompts not to find this command is entered in Android/out/host/linux-x86/bin/./ADB Shell

Enter PS to check whether the addserver process exists.

If there is one, half is successful.

 

Test our services
 

Create a simple application in Android, packages, and apps,

Here, you can directly create a project in eclipse and copy it to Android/packages/apps, and then add an android. mk file to the application, which can be copied from other applications for modification.

Test the service code in the application:

Private void test (){

Try {

Ibinder binder = servicemanager. getservice ("guilh. Add"); // get the service

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); // input parameters

Binder. transact (0, Data, reply, 0); // execute remote call

Log. D (TAG, "result =" + reply. readint (); // verify the result

} Catch (exception e ){

Log. D (TAG, E. tostring ());

}

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/belyxiong/archive/2010/09/10/5875993.aspx

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.