1、service部分
android-7-0-0_r6\system\core\healthd\BatteryPropertiesRegistrar.h
android-7-0-0_r6\system\core\healthd\BatteryPropertiesRegistrar.cpp
void BatteryPropertiesRegistrar::publish(
const sp<BatteryPropertiesRegistrar>& service) {
defaultServiceManager()->addService(String16("batteryproperties"), service);
}
2、server部分
android-7-0-0_r6\system\core\healthd\healthd_mode_android.cpp
void healthd_mode_android_init(struct healthd_config* /*config*/) {
ProcessState::self()->setThreadPoolMaxThreadCount(0);
IPCThreadState::self()->disableBackgroundScheduling(true);
IPCThreadState::self()->setupPolling(&gBinderFd);
if (gBinderFd >= 0) {
if (healthd_register_event(gBinderFd, binder_event))
KLOG_ERROR(LOG_TAG,
"Register for binder events failed\n");
}
gBatteryPropertiesRegistrar = new BatteryPropertiesRegistrar();
gBatteryPropertiesRegistrar->publish(gBatteryPropertiesRegistrar);
}
android-7-0-0_r6\system\core\healthd\healthd.cpp
static struct healthd_mode_ops android_ops = {
.init = healthd_mode_android_init,
.preparetowait = healthd_mode_android_preparetowait,
.heartbeat = healthd_mode_nop_heartbeat,
.battery_update = healthd_mode_android_battery_update,
};
int main(int argc, char **argv) {
int ch;
int ret;
klog_set_level(KLOG_LEVEL);
healthd_mode_ops = &android_ops;
if (!strcmp(basename(argv[0]), "charger")) {
healthd_mode_ops = &charger_ops;
} else {
while ((ch = getopt(argc, argv, "cr")) != -1) {
switch (ch) {
case 'c':
healthd_mode_ops = &charger_ops;
break;
case 'r':
healthd_mode_ops = &recovery_ops;
break;
case '?':
default:
KLOG_ERROR(LOG_TAG, "Unrecognized healthd option: %c\n",
optopt);
exit(1);
}
}
}
ret = healthd_init();
if (ret) {
KLOG_ERROR("Initialization failed, exiting\n");
exit(2);
}
healthd_mainloop();
KLOG_ERROR("Main loop terminated, exiting\n");
return 3;
}
android-7-0-0_r6\system\core\healthd\Android.mk
include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
healthd.cpp \
healthd_mode_android.cpp \
healthd_mode_charger.cpp \
BatteryPropertiesRegistrar.cpp
LOCAL_MODULE := healthd
LOCAL_MODULE_TAGS := optional
LOCAL_FORCE_STATIC_EXECUTABLE := true
LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT_SBIN)
LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)
LOCAL_CFLAGS := -D__STDC_LIMIT_MACROS -Werror
ifeq ($(strip $(BOARD_CHARGER_DISABLE_INIT_BLANK)),true)
LOCAL_CFLAGS += -DCHARGER_DISABLE_INIT_BLANK
endif
ifeq ($(strip $(BOARD_CHARGER_ENABLE_SUSPEND)),true)
LOCAL_CFLAGS += -DCHARGER_ENABLE_SUSPEND
endif
LOCAL_C_INCLUDES := bootable/recovery
LOCAL_STATIC_LIBRARIES := libbatterymonitor libbatteryservice libbinder libminui libpng libz libutils libcutils liblog libm libc
ifeq ($(strip $(BOARD_CHARGER_ENABLE_SUSPEND)),true)
LOCAL_STATIC_LIBRARIES += libsuspend
endif
LOCAL_HAL_STATIC_LIBRARIES := libhealthd
# Symlink /charger to /sbin/healthd
LOCAL_POST_INSTALL_CMD := $(hide) mkdir -p $(TARGET_ROOT_OUT) \
&& ln -sf /sbin/healthd $(TARGET_ROOT_OUT)/charger
include $(BUILD_EXECUTABLE)
android-7-0-0_r6\bootable\recovery\etc\init.rc
import /init.recovery.${ro.hardware}.rc
on early-init
# Set the security context of /postinstall if present.
restorecon /postinstall
start ueventd
start healthd
service healthd /sbin/healthd -r
critical
seclabel u:r:healthd:s0
3、改进
据此改进《Android native service实例(一)》
在android-7-0-0_r6\system\core\rootdir\init.rc下添加如下代码,将service添加到启动文件中。
on boot
# basic network init
......
class_start core
start ThinkingServer
# yuyong
service ThinkingServer /system/bin/ThinkingServer
class core
critical
然后重新编译,刷system镜像