最近工作原因已经开始由Android软件转向Android系统进行转变。目前碰到很多问题,自己在CSDN进行记录相关问题,加深印象。提高自己自己学习效率。
Android系统启动后Zygote进程会fork SystemServer进程。SystemServer.java文件位于 /base/services/java/com/android/server/SystemServer.java。
SystemServer 启动Andorid很多服务如:AMS,PMS等等。主要是通过下面三个方法启动:
// Start services.
try {
traceBeginAndSlog("StartServices");
startBootstrapServices();
startCoreServices();
startOtherServices();
SystemServerInitThreadPool.shutdown();
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
} finally {
traceEnd();
}
今天记录问题是在裁剪Android系统过程中有些不需要的服务是可以不启动的。比如说我不需要WifiP2P服务功能,这部分功能其实通过下面代码进行判断是否要启动的:
if(context.getPackageManager().hasSystemFeature
(PackageManager.FEATURE_WIFI_DIRECT)) {
traceBeginAndSlog("StartWifiP2P");
mSystemServiceManager.startService(WIFI_P2P_SERVICE_CLASS);
traceEnd();
}
PackageManager通过hasSystemFeature判断是否需要启动WifiP2P。其实是从SystemConfig读取到的mAvailableFeatures中查询是否存在,或者版本更新。mAvailableFeatures是一个ArrayMap对象。其中value是通过Android手机系统目录下system下的etc文件。我个人使用代码是Android 8.1 _r34代码。其实上述目录是拷贝frameworks/native/data/etc下面的xml文件。不同厂商也有可能放置在不同位置比如system/etc/permissions等。如果删除这个xml,就会导致这个服务不能启动。上诉WIFI热点功能对应xml文件为android.hardware.wifi.direct.xml。文件如下:
<!-- Copyright (C) 2011 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- This is the standard feature indicating that the device includes WiFi Direct. -->
<permissions>
<feature name="android.hardware.wifi.direct"/>
</permissions>
这些文件会再编译的mk文件中配置。将其拷贝到out目录下,一般情况主要是build、vendor还有就是device下的的mk文件。而我需要做的事情就是删除mk里面相关拷贝配置。从而达到屏蔽相关不需要服务。