Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Android Service basic framework

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use the basic framework of Android Service". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the basic framework of Android Service.

Service in-depth analysis

There are three sense services in the Android architecture:

Native service

Android service

Init space services, mainly property settings, this IPC is done using Socket, which I will discuss in another chapter.

Navite service actually refers to the services completed entirely in C++ space, mainly refers to the services initialized by the system and scripted through Init.rc, such as Service Manger service,Zygote service,Media service, ril_demon service and so on.

Android service refers to the service completed in the JVM space, although it also uses the framework on Navite, but the service subject exists in the Android space. Android is a service established during the initialization of the second phase initialization (Init2).

1 Service essential structure

Let's start with the analysis of the fundamental meaning of Service. The essence of a service is to respond to client requests. To provide services, it is necessary to establish a framework for receiving requests, processing requests, and responding to customers. I think Android Service designers will always keep the essential block diagram of this service in mind. From a program point of view, the service must have a closed loop framework and a request processing framework.

To analyze the service frame clearly, it is necessary to understand the following mechanisms and their composition.

(1) where is the closed loop structure placed?

(2) how are requests distributed and managed?

(3) how is the processing framework established?

(4) how is the conceptual framework established?

2 Analysis of the basic framework of Service

In the design of Android, Native Service and Android Service adopt the same closed loop framework. This closed loop frame is placed in the C++ space in Native, and the ProcessState@ProcessState.cpp and IPCThreadState@IPCThreadState.cpp classes do all the work.

ProcessState is placed in the global variable gProcess, and each process has only one ProcessState object, which is responsible for opening the Binder device driver, establishing thread pools, and so on. While IPCThreadState has one per thread, IPCThreadState instances are registered in the context-dependent data of Binder threads, and are mainly responsible for Linux data reading, writing and request processing framework. When IPCThreadSate is constructed, it gets the ProcessSate of the process and records it in its member variable mProcess, and the handle of Binder can be obtained through mProcess.

3.1Lifecycle of ProcessState

Since ProcessState is the foundation of Binder communication, Process must be established before Binder communication. Both the client and the server must be established. Since we now focus on the server side, the focus is on the server side. In the Android system, there are C++ space services and JVM space services, these two kinds of services are the same in essence, but different in form, because they are both based on ProcessState, so the difference in form is only shown in the callback handling of OnTransact.

Native Service

We can directly see the use of sp proc (ProcessState::self ()) to establish the ProcessState, which is established once the ProcessState is called, and this self registers the ProcessSate in the global variable.

Android Service

We can see the same structure in setting up the Android Service service system_init @ System_init.cpp. One difference is that all Android Service runs in one process: the systemsever process.

3.2 Binder Driver Packaging @ IPCThreadState

When ProcessSate is constructed, open / driver/binder with open_binder and record the handle in mDriverFD. This handle is not used in ProcessState. It is IPCThreadState that really uses this Binder device handle, and all operations about Binder are placed in IPCThreadState:

(1) read / write: talkWithDriver () @ IPCThreadState wraps ioctl (mProcess- > mDriverFD, BINDER_WRITE_READ, & bwr).

(2) request processing: executeCommand (...) @ IPCThreadState

(3) Loop structure: joinThreadPool ()

JoinThreadPool ()

{

While (1) {

TalkWithDriver (...)

...

ExecuteCommand (...)

}

}

At this point, I believe you have a deeper understanding of "how to use the basic framework of Android Service". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report