[Android development] Android services and system services

Services and system services in Android

  • Android service: A component that runs in the background and performs long-running tasks that do not require user interaction. As an application component in Android development, it is used by inheriting the extern Service class.

  • Android system service: The service that runs as the Andorid system starts is divided into Java system services and Native system services.

Add Java system service

http://qiushao.net/2019/12/20/Android%E7%B3%BB%E7%BB%9F%E5%BC%80%E5%8F%91%E5%85%A5%E9%97%A8/7-%E6%B7%BB%E5%8A%A0java%E7%B3%BB%E7%BB%9F%E6%9C%8D%E5%8A%A1/

  1. Use aidl to define the service interface
    Write IHelloService.aidl,
    Execute the mm -j command to compile the framework.jar module
    After successful compilation, the file IHelloService.java will be generated

  2. Implement the interface
    Implement the HelloService.java file

  3. Add services to ServiceManager
    Modify the SystemServer.java file and add code to the startOtherServices method

  4. Set selinux rules

  5. Compilation verification

  6. Client call

import android.pure.IHelloService;
……
service = IHelloService.Stub.asInterface(ServiceManager.getService("HelloService"));

Add Native system service

http://qiushao.net/2019/12/29/Android%E7%B3%BB%E7%BB%9F%E5%BC%80%E5%8F%91%E5%85%A5%E9%97%A8/9-%E6%B7%BB%E5%8A%A0native%E7%B3%BB%E7%BB%9F%E6%9C%8D%E5%8A%A1/

The previous section is the implementation of Java layer system services. Sometimes due to performance considerations, some services need to be implemented in C++, such as audio and video encoding and decoding, graphics drawing, etc. The Android system's native MediaPlayerService and SurfaceFlinger are Native system services implemented in C++.

  1. Declare the service interface
    Write HelloNativeService.h
class HelloNativeService: public BBinder {
    
    
public:
    HelloNativeService();
    static int instantiate();
    virtual status_t onTransact(uint32_t, const Parcel&, Parcel*, uint32_t);

};
  1. Implement the service function
    Create the file HelloNativeService.cpp
#include "HelloNativeService.h"

static void sayHello(const char *name) {
    
    
    LOGD("hello %s from HelloNativeService", name);
}

static int sum(int a, int b) {
    
    
    return a + b;
}

HelloNativeService::HelloNativeService() {
    
    
    LOGD("HelloNativeService created");
}

status_t HelloNativeService::onTransact(uint32_t code, const Parcel &request, Parcel *reply, uint32_t flag) {
    
    
    switch (code) {
    
    
        case CMD_SAY_HELLO:
            sayHello(request.readCString());
            return NO_ERROR;

        case CMD_CAL_SUM:
            int a = request.readInt32();
            int b = request.readInt32();
            reply->writeInt32(sum(a, b));
            return NO_ERROR;
    }
    return BBinder::onTransact(code, request, reply, flag);
}
  • code: Indicates the action to be performed, similar to the what of the Message sent by the Handler. code indicates the current remote operation command, and IBinder defines several common commands like INTERFACE_TRANSACTION and PING_TRANSACTION. The identity value of the command you use needs to be between FIRST_CALL_TRANSACTION and LAST_CALL_TRANSACTION.
  • request, reply: The request and reply parameters are equivalent to the calling parameters and return values ​​in ordinary functions. The Parcel type is data that can cross processes.
  • flag: There are only two parameter flags: 0 and FLAG_ONEWAY. 0 means blocking call, FLAG_ONEWAY means asynchronous call. This parameter does not need to be processed on the server side, the Binder framework will handle it automatically.
  1. Add main.cpp
    We just defined the service interface earlier. This service also needs to depend on a certain process to run

  2. Writing Android.bp

  3. Set selinux rules

  4. Set to run automatically at boot

  5. Write a client to call the service

  6. Compile and run


Reference: https://bbs.huaweicloud.com/blogs/311242

Guess you like

Origin blog.csdn.net/qq_39441603/article/details/133861072