Top Banner
How to Create a System Service in Android Sheng-Wei, Lin 2015/02/11
28
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: How to Create a System Service in Android

How to Create a System Service in Android

Sheng-Wei, Lin2015/02/11

Page 2: How to Create a System Service in Android

Introduction to System Service Getting Start to Create a System Service

◦ Case Study - Hello Service◦ Hello Driver ◦ Hello Service & Hello Manager

Demo Conclusions

Outline

Page 3: How to Create a System Service in Android

Plays a key role in exposing the low-level functions of the hardware and the Linux kernel to the high-level applications.

Lives from boot to reboot shell@acer_a1_724:/$ service list

System Service

Page 4: How to Create a System Service in Android

Android System Architecture

Page 5: How to Create a System Service in Android

Service Manager & Binder

Page 6: How to Create a System Service in Android

Creating a system service is platform-dependent!

Hello service as our Example Platform version

◦ Android 4.4 After ending of the presentation, we use

HelloTest application to verify the functionality of Hello Service

Getting Start to Create a System Service

Page 7: How to Create a System Service in Android

Working Mechanism of Hello Service

JNIJava

C/C++

Apps

android.os.HelloMagager1

System ServerHello

Service

Hw lib loader

hello.msm8916.so

hello driver

Binder

User space

Kernel space

/dev/hello

dlopen()

Page 8: How to Create a System Service in Android

A character device Provides r/w operation to Access variable

hello_var through /dev/hello

Hello Driver

Page 9: How to Create a System Service in Android

frameworks/base/services/java/com/android/server/HelloService.java Invoke native functions

Hello Service

Page 10: How to Create a System Service in Android

frameworks/base/services/jni/com_android_server_HelloService.cpp Implement

◦ Method table

◦ Methods

JNI – Method Implementation

Page 11: How to Create a System Service in Android

frameworks/base/services/jni/com_android_server_HelloService.cpp Register Hello’s native methods to Dalvik

frameworks/base/services/jni/onload.cpp

JNI – Registration

JNI_OnLoad

Page 12: How to Create a System Service in Android

frameworks/base/services/jni/com_android_server_HelloService.cpp hw_get_module => dlopen(“/system/lib/hw/hello.msm8916.so”, RTLD_NOW)

=> dlsym(handle, “HMI”); //Find the symbol HMI

JNI – Load HAL module

Page 13: How to Create a System Service in Android

hardware/qcom/hello/hello.cpp hello_module_t

hardware/libhardware/include/hardware/hardware.h

Hello HAL Module

Page 14: How to Create a System Service in Android

hardware/qcom/hello/hello.cpp hello_module_t

Hello HAL interface

Page 15: How to Create a System Service in Android

hardware/qcom/hello/hello.cpp

Hello HAL Module

Page 16: How to Create a System Service in Android

hardware/qcom/hello/hello.cpp hello_device_t

Hello HAL interface

Page 17: How to Create a System Service in Android

So far … we have gone through …

Working Mechanism of Hello Service

Binder

/dev/hello

JNIJava

C/C++

Apps

android.os.HelloMagager1

System ServerHello

Service

Hw lib loader

hello.msm8916.so

hello driver

User space

Kernel space

dlopen()

Page 18: How to Create a System Service in Android

Create a Hello Manager for the use of applications◦ Applications could acquire the Hello Manager by

invoking getSystemService(HELLO_SERVICE); Hello Manager interacts with Hello Service

through Binder Android Interface Definition Language (AIDL)

◦ an IDL language used to generate code that enables two processes on an Android-powered device to talk using interprocess communication (IPC).

Communication between Hello Service & Application

Page 19: How to Create a System Service in Android

Create IHelloService1.aidl Generate IHelloService1.java

◦ shell@acer_a1_724:/$ aidl IHelloService1.aidl Implement the interface

AIDL - Steps to Implement IPC

Page 20: How to Create a System Service in Android

frameworks/base/core/java/android/os/IHelloService1.aidl

AIDL - Create IHelloService1.aidl

package android.os;

/** {@hide} */interface IHelloService1{ int read(); int write();}

Page 21: How to Create a System Service in Android

frameworks/base/core/java/android/os/IHelloService1.java

……………

AIDL – Generate IHelloService1.java

Page 22: How to Create a System Service in Android

frameworks/base/services/java/com/android/server/HelloService.java

AIDL - Implement the IHelloService1

Page 23: How to Create a System Service in Android

Hello Manager frameworks/base/core/java/android/os/HelloManager1.java

Page 24: How to Create a System Service in Android

Last two steps ◦ Add Hello Service to Service Manager

frameworks/base/services/java/com/android/server/SystemServer.java

◦ Release Hello Serviceframeworks/base/core/java/android/app/ContextImpl.java

Finally..

Page 25: How to Create a System Service in Android

packages/apps/helloapp/src/com/example/hellotest/MainActivity.java

Application - HelloTest

import android.os.HelloManager1;

public class MainActivity extends Activity { private HelloManager1 hm; public void onStart() { hm =(HelloManager1)getSystemService(HELLO_SERVICE); }

……………………………}

Page 26: How to Create a System Service in Android

Demo

Page 27: How to Create a System Service in Android

We used a hello service as our example to illustrate how to create a system service in Anroid

We used HelloTest app to verify the functionality of hello service

Conclusions

Page 28: How to Create a System Service in Android

[1] Karim Yaghmour, “嵌入式 Android系統 ,” 碁峰資訊 , 台北市 , 2013年 10月

[2] Android Developers - Training◦ http://developer.android.com/training/index.html

[3] Binder : Communication Mechanism of Android Processes◦ http

://www.cubrid.org/blog/dev-platform/binder-communication-mechanism-of-android-processes/

[4] Android Low-Level System Architecture◦ https://source.android.com/devices/

[5] Implementing IPC Using AIDL◦ http://www.linuxtopia.org/online_books/android/devguide/guide/

developing/tools/aidl.html#exposingtheinterface

References