Explanation of various types of services in the Android system

Source: Internet
Author: User

Explanation of various types of services in the Android system

Android has three categories of services, which are at different framework layers of the android system. The specific explanation is as follows:


1) service in init. rc // keyword "service" name binary executable program path

Service servicemanager/system/bin/servicemanager

Class core

User system

Group system

Critical

Onrestart restart healthd

Onrestart restart zygote

Onrestart restart media

Onrestart restartsurfaceflinger

Onrestart restart drm

In fact, this type of service is to define the program to start. The object of this type of service is a binary program that can be executed. It is defined as a service to better describe the attributes of the binary running, such as running users, groups, starting once (oneshot) or continuously starting (the program will start again after it dies ). More importantly, it can also describe the relationship between various programs. For example, onrestart restart drm, which means that when the drm program is restarted, it also needs to be restarted.

At the same time, you can use the following command to enable or disable a service dynamically at runtime. Of course, you must have the root permission.

Property_set ("ctl. start", "servicemanager"); property_set ("ctl. stop", "servicemanager ");

Adb shell setprop ctl. start servicemanager; adb shell setprop ctl. stop servicemanager


2) system layer service

This type of service is very similar to the service we generally understand, that is, the service provided in the system. This type of service defines a service interface, and other modules can use this service through this interface. The execution body of this type of service can be binary code or java code. For example:

C ++ type service:

SurfaceFlinger, CameraManagerService, MediaPlayerService

Java-type service:

ActivityManagerService, WindowManagerService, NetworkManagerService

The implementation mechanism is that servicemanager mentioned above registers its service. Other modules obtain the service interface from servicemanager by name. In shell, you can run the "service list" command to list all the services in the system.

C ++ service interfaces:

sp<IServiceManager> sm = defaultServiceManager();sm.addService(name, xx)/sm.getService(name)

Java-type service interfaces:

ServiceManager. getService (name)/addService (name, xx)

3) SDK-layer service

Unlike the previous two types of services, they are a general term for a class of objects, and such services are a very specific java class android. app. service. java that developers often come into contact. It is exposed to third-party applications by the android sdk. It is equivalent to Activity and is one of the four android components (Activity, Service, Provider, BroadcastReceiver ).


Summary:

Category 1 service: system integration engineer

Category 2 service: framework system engineer

Category 3 service: Daily close contact with android Application developers


How can I write a Service on android? Do not call the System Service

The application to call this Service
Thread is only used in this class. If this class is closed, this thread will be laid off.
But the Service class won't. You will understand an example of Baidu's simple boot prompt.
After the instance is started, the Service is actually running, and the Thread must be called by you.
No help

How to Use ServiceTestCase to test and answer Android Service APIs

This class contains the permission to test the application, and controls a large number of methods such as the tested application and Service. It also provides simulated applications (Applcation) and Context so that the Service can be tested independently of the application. Service TestCase. the setUp () method is executed before each test case call. When the method is executed, configure the test data to copy and obtain the Context provided by the current system. you can use getSystemContext () to obtain the current Context of the system. If you override the setUp () method, the first statement should be super. setUp (). The setApplication (Application) method and setContext (Context) method allow you to set the simulated Context and the simulated Application before the Service starts. If this parameter is not set, MockApplication and MockContext are automatically specified for the test. When startService () or bindService () is called, ServiceTestcase will automatically initialize the test environment. Therefore, to set a specific test environment, you must create an object to be simulated before starting the tested Service. Please note that ServiceTestCase. bindService () method and Service. the parameters of the bindService () method are different: ServiceTestCase. the bindService () method only needs to provide the Intent object, while the Service. bindService () also needs to specify ServiceConnection and flag. In addition, the ServiceTestCase. bindService () method returns a subclass of an IBinder object, while Service. bindService () returns a Boolean value. After each test case ends, the teardown () method is recycled according to the Service life cycle. For example, when a Service is started using the bindService () method, the teardown () method automatically calls the onUnbind () method and onDestroy () method of the Service in sequence. By default, ServiceTestCase executes the testAndroidTestCaseSetupProperly () method. It is used to verify whether the context is successfully set before other test cases are run. The following uses the API test of the Security component of SAF as an example to describe the test process. First, the API provided by the Security component is exposed in the AIDL method using int getAgentVersionCode () string exec (String [] args, String directory, boolean noResult, boolean isSync). Therefore, you must use the bindService () method of ServiceTestCase for testing. First, create a test project and modify AndroidManifest. xml <application android: icon = "@ drawable/ic_launcher" android: label = "@ string/app_name"> <uses-library android: name = "android. test. runner "> </uses-library> </application> <instrumentation android: name = ". testR ...... remaining full text>

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.