How to add system services in Android 4.0.3

Source: Internet
Author: User

 

Reference: http://blog.csdn.net/belyxiong/article/details/5875993 adds underlying core services for Android

The information in the blog post is incomplete and has a small problem. This article will make some preparations for the entire process and complete and correct the content.

 

User @ Ubuntu:/$ sudo ADB Shell

Root @ Android:/# cd system/lib --> service library file

Root @ Android:/# cd system/bin --> Service Executable File

Root @ Android:/# Cat init. RC --> service configuration file

 

Service Startup Process:

Add the service in init. RC to call the executable file of the service and add the service to servicemanager. In this way, the application can obtain the service method.

 

The current Ubuntu directory is the root directory of the entire android4.0 project, including subdirectories such as frameworks, system, and packages.

 

1. Custom Service myservice Library

$ Mkdir-P frameworks/base/myservice/libmyservice

$ CD frameworks/base/myservice/libmyservice

$ Touch Android. mk myservice. h myservice. cpp

 

Android. mk content:

Local_path: = $ (call my-DIR) include $ (clear_vars) local_src_files: = myservice. cpplocal_module: = partition: = optionallocal_c_includes :=\$ (jni_h_include) Partition :=\ libcutils \ libutils \ libbinder \ tags: = falseinclude $ (build_shared_library) # This line indicates compiling as a dynamic

 

Note: The local_module_tags line should be written in the middle of two include $

 

Myservice. H content:

# Ifndef android_guilh_my_service_h # define android_guilh_my_service_h # include <utils/refbase. h> # include <binder/iinterface. h> # include <binder/parcel. h> # include <utils/threads. h> namespace Android {class myservice: Public bbinder {// derived from bbinder, implements the local interface public: static int instantiate (); myservice (); Virtual ~ Myservice (); Virtual status_t ontransact (uint32_t, const parcel &, parcel *, uint32_t) ;};// namespace # endif

 

Myservice. cpp content:

# Include "myservice. 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 myservice: instantiate () {LogE ("myservice: instantiate "); // Add // myserice to the binder driver. The service name is guilh. myservice int r = defaservicservicemanager ()-> addservice (string16 ("guilh. myservice "), new myservic E (); LogE ("myservice r = % d/N", R); return r;} // constructor myservice: myservice () {LogE ("myservice created"); pthread_key_create (& sigbuskey, null);} // destructor myservice ::~ Myservice () {pthread_key_delete (sigbuskey); LogE ("myservice destroyed") ;}// this is a specific local implementation of the Service, where all functions should be implemented, different operations are performed by passing in the Execution Code (Code) //. The upper layer hides different APIs. Status_t myservice: ontransact (uint32_t code, const parcel & Data, parcel * reply, uint32_t flags) {LogE ("myservice: ontrascat"); Switch (CODE) {// only process the case where code = 0 case 0: {// read the integer in data and add 1000 to it, then write the result to reply 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

 

Then use Mmm to compile and generate libmyservice. So,

$ ADB push out/target/product/XXX/system/lib/libmyservice. So/system/lib/

Replace xxx above the project.

2. Custom Service myservice executable file, myserver

$ Mkdir-P frameworks/base/myservice/myserver

$ CD frameworks/base/myservice/myserver

$ Touch Android. mk myserver. cpp

Android. mk content:

Local_path: = $ (call my-DIR) include $ (clear_vars) local_src_files: = myserver. cpplocal_shared_libraries :=\ libmyservice \ libutils \ libbinderlocal_module: = myserverlocal_module_tags: = optionalinclude $ (build_executable) # compile as an executable file

 

Myserver. cpp 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 ".. /libmyservice/myservice. H "using namespace android; int main (INT argc, char ** argv) {LogE (" myserver: Main "); sp <processstate> proc (processstate :: self (); sp <iservicemanager> Sm = defaultservicemanager (); // obtain servicemanager LogE ("servicemanager: % P", SM. get (); myservice: instantiate (); // Add yourself to servicemanager processstate: Self ()-> startthreadpool (); // start the buffer pool ipcthreadstate :: self ()-> jointhreadpool (); // Add the service to the binder Closed Loop process}

 

Then, use Mmm to compile and generate myserver,

$ ADB push out/target/product/XXX/system/bin/myserver/system/bin/

 

3. Modify init. RC.

$ CD system/CORE/rootdir

Edit init. RC and add:

# Myservice

Service myserver/system/bin/myserver

Class main

User Root

$ Make bootimage

$ Sudo ADB reboot bootloader

$ Sudo fastboot Flash boot out/target/product/XXX/boot. img

$ Sudo fastboot reboot

 

4. Run the myservice Test Program

$ CD Packages/apps/

$ Android list

Replace the following command ID with an appropriate value based on the result.

$ Android Create project -- target ID -- name myservicetest -- path myservicetest -- activity myservicetestactivity -- package com. example. MyApp

$ CD myservicetest

$ VI Android. mk

LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS := optionalLOCAL_SRC_FILES := $(call all-java-files-under, src)LOCAL_PACKAGE_NAME := MyServiceTestLOCAL_CERTIFICATE := platform#LOCAL_SDK_VERSION := currentLOCAL_PROGUARD_FLAGS := -include $(LOCAL_PATH)/proguard.flagsinclude $(BUILD_PACKAGE)# Use the following include to make our test apk.include $(call all-makefiles-under,$(LOCAL_PATH))

$ VI Res/layout/Main. xml

Delete the Android: text line in <textview>. Otherwise, the language compilation fails.

Or add tests: local_module_tags: = optional tests to Android. mk.

 

$ VI src/COM/example/MyApp/myservicetestactivity. Java

Package COM. example. myApp; import android. app. activity; import android. OS. bundle; import android. OS. ibinder; import android. OS. parcel; import android. OS. servicemanager; import android. OS. process; import android. util. log; public class myservicetestactivity extends activity {/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); log. D ("myservice", "myservice: oncreate"); setcontentview (R. layout. main); test ();} private void test () {try {log. D ("myservie", "myservice: test"); ibinder binder = servicemanager. getservice ("guilh. myservice "); // get the service parcel DATA = parcel. obtain (); parcel reply = parcel. obtain (); If (Binder = NULL) log. D ("myservice", "failed to get service"); data. writeint (process. mypid (); // fixed operation data. writeint (100); // input the binder parameter. transAct (0, Data, reply, 0); // execute remote call log. D ("myservice", "result =" + reply. readint (); // verification result} catch (exception e) {log. D ("myservice", E. tostring ());}}}

Then, use Mmm to compile and generate myservicetest,

$ ADB install-r out/target/product/XXX/system/APP/myservicetest.apk

 

Finally, run the myservicetest program on your mobile phone,

You can view the log through logcat-s myservice to find that the service is running and the service is obtained successfully after the test.

 

Related Article

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.