Book Image

Instant Android Systems Development How-to

By : Earlence Fernandes
Book Image

Instant Android Systems Development How-to

By: Earlence Fernandes

Overview of this book

<p>Android is by far the most popular open source mobile operating system. Learning to write high quality code at the platform level and learning how the systems works internally is a vital skill. This book teaches you these skills with clear and concise explanations and code examples.</p> <p>Instant Android Systems Development How-to provides a gentle introduction to the platform internals without sacrificing depth. Source code examples are designed to be meaningful, but at the same time, do not disguise their real purpose, which is to illustrate systems development techniques and common design patterns in android systems programming. Readers will be guided through several examples that give a hands-on experience.</p> <p>Readers begin by downloading the android source code, which is a topic of much discussion on android forums. They are then guided through the android boot process, and later on learn various common android systems development paradigms. More importantly, the book provides advice on when to use certain techniques which is often a mystery for the novice developer. Readers who complete the book will have high confidence in developing good systems code for Android.</p> <p>The book discusses how to setup a development machine and how to obtain the android source code and kernel code. It describes the source code organization and how the system boots up with precise references to various points in the source code. It highlights the common systems design patterns followed and how to create a custom system service. It then covers the all important flashing of phones. This is a topic of much confusion and the book provides direct steps to achieve safe flashing of developer phones. It describes the user application library mechanism and the platform library mechanism. Native code is needed for certain operations and an example service utilizing native code is explained. Modification of core system applications is explained and useful tips are provided on how to speed up the build-test cycle. The book concludes with a case study of two real world android platform extensions which give the user a reference while developing their own extensions.</p> <p>Instant Android Systems Development How-to is a well rounded book on platform internals that provides simple explanations without sacrificing depth and rigor.</p>
Table of Contents (7 chapters)
Instant Android Systems Development How-to
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
Instant Android Systems Development How-to

Adding a custom service to the SystemServer process (Must know)


We need to register our service with the Android system and create an object. The Service Manager is a component that maintains a mapping of the service name and the associated service object. Processes invoke the Service Manager to obtain a reference to the system server by name. The method invoked to obtain a reference to a service object is ServiceManager.getService(String). You can think of the Service Manager as a directory service that is available to service consumers.

Getting ready

We will add our custom server to SystemServer.java located at ANDROID_SRC/frameworks/base/services/java/com/android/server.

How to do it…

The following code represents modifications you need to make to the SystemServer.java file. Locate the run() method and add the following lines at an appropriate location. For the purpose of illustration, we choose to add this after all services have been started.

...
//begin packt 
Slog.i(TAG, "PacktCrypto service"); 
com.android.packt.PacktCrypto pcrypt = com.android.packt.PacktCrypto.getInstance(); 
ServiceManager.addService("PacktCryptoService", pcrypt); 
//end packt
…

How it works…

The preceding code modification obtains a reference to an object of type PacktCrypto. It then adds that object to the ServiceManager class, which, if you recall, is a directory service for all system services. It adds the PacktCrypto object to the directory by invoking the addService() method that takes as arguments a string service identifier and the object itself.

In the code fragment, we must create an object of PacktCrypto and add it to the Service Manager directory with a string name. We choose PacktCryptoService for our example. At this stage, our custom server will be created and registered with the Service Manager.