Tutorial on how to add services to Android system
preface
Recently, because the company's platform is moving from Android 4.4.4 to Android 6.0, the problem is that we have added some services to the system before, so we need to migrate some system level services and some custom packages of the framework
I happened to see the patch of adding system services on Gerrit. It's just a summary. Although I'm not a framework engineer, it's good to know about Android system
How to obtain system services
We get the system services in the context and getsystemservice. Let's take a look at what happened to getsystemservice
The implementation of getsystemservice is contextimpl. Let's take a look at the source code of contextimpl
Android 4.4.4 (KitKat)
Here is the source code of Android 4.4.4 and 6.0. See it later
We also see many static code blocks in contextimpl. They are all registered services, and they are all our common system services
So, isn't this where we sign up for the service?
So. We found the place to register system services. Here, we just need to add the services we want to register and complete the abstract method of new servicefetcher(). In this way, we can get our service object by getsystemservice and passing in the name at the time of registration. Of course, this is the method of 4.4
Android 6.0 (Marshmallow)
Let's look at the code of contextimpl
We found that, unlike KitKat, marshmallow is obtained from a class called systemserviceregistry
Well, let's look at its source code. It's the same routine as before, but it encapsulates a class to manage these registered services. This design is really good. The coupling degree of the code looks much smaller, and it won't make the contextimpl class more and more bloated
So. Our marshmallow system service should be added to the systemserviceregistry class in the same way. Then we can get our service object by getsystemservice
summary
The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.