Android开机自启动添加

本文介绍如何在Android设备上配置自启动服务,包括创建可执行文件、设置打包路径、添加开机自启动权限及在init.rc中定义服务等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、添加需要自启动的可以执行文件:
(1)可执行C文件:system/core/init_start/needInitStartService.c
例如:

#include <stdio.h>
#include <cutils/properties.h> 
int main()
{
	FILE * fp;
	char buffer[16];
	fp=popen("/system/bin/modetool -s | grep +QCSN: | sed 's/\"/ /g' | awk '{print $2}'","r");
	fgets(buffer,sizeof(buffer),fp);
	property_set("persist.sys.getSNumber",buffer);
	pclose(fp);
    return 0;
}

(2)打包Android.mk文件system/core/init_start/Android.mk

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)
#LOCAL_FORCE_STATIC_EXECUTABLE := true

LOCAL_SRC_FILES:= \
    drmservice.c

LOCAL_MULTILIB := 32

LOCAL_C_INCLUDES += bionic \
$(call include-path-for, libhardware_legacy)/hardware_legacy

LOCAL_MODULE:=needInitStartService

LOCAL_MODULE_TAGS := optional

LOCAL_STATIC_LIBRARIES := libfs_mgr libcutils 
#libc

LOCAL_SHARED_LIBRARIES := libhardware_legacy libnetutils liblog

include $(BUILD_EXECUTABLE)

2、添加可执行文件needInitStartService打包到固件操作
device/qcom/msmXXXX_64/msmXXXX_64.mk

+   PRODUCT_PACKAGES += needInitStartService

    PRODUCT_COPY_FILES += $(call find-copy-subdir-files,*,device/qcom/XXXX_64/xsy,data/xsy)

    PRODUCT_COPY_FILES += $(call find-copy-subdir-files,*,device/qcom/msmXXXX_64/xsy,vendor/app/xsy)

3、添加开机自启动权限
(1)、file_contexts中添加device/qcom/sepolicy/vendor/msmXXXX/file_contexts

    /dev/block/platform/soc/7824900.sdhci/by-name/sbl1_[ab]             u:object_r:xbl_block_device:s0
+   /system/bin/needInitStartService                        u:object_r:needInitStartService_exec:s0

(2)、具体权限添加,新建此文件device/qcom/sepolicy/vendor/msmXXXX/needInitStartService.te

# needInitStartService service
type needInitStartService, domain,coredomain;
type needInitStartService_exec, exec_type,file_type;

init_daemon_domain(needInitStartService)

allow needInitStartService shell_exec:file {execute read open execute_no_trans getattr};
allow needInitStartService property_socket:sock_file {write};
allow needInitStartService init:unix_stream_socket {connectto};
allow needInitStartService system_file:file { execute_no_trans };
allow needInitStartService system_data_file:file { getattr };
allow needInitStartService toolbox_exec:file { getattr execute read open execute_no_trans};
allow needInitStartService vendor_toolbox_exec:file { getattr execute};
allow needInitStartService smd_device:chr_file {  read write open};
allow needInitStartService system_prop:property_service {  set};

3、init.rc将启动文件添加成service在系统开机后自启动:

    # This allows the ledtrig-transient properties to be created here so
    # that they can be chown'd to system:system later on boot
    write /sys/class/leds/vibrator/trigger "transient"
+	chmod 777 /system/bin/needInitStartService
+	setprop persist.sys.getSNumber 000000000000000

# Healthd can trigger a full boot from charger mode by signaling this
# property when the power button is held.
    class_reset late_start
    class_reset main


+service needInitStartService/system/bin/needInitStartService
+   class main
+   oneshot

on property:sys.boot_completed=1
    bootchart stop
+	start needInitStartService

# system server cannot write to /proc/sys files,
# and chown/chmod does not work for /proc/sys/ entries.

## Daemon processes to be run by init.
##


service ueventd /sbin/ueventd
    class core
    critical

至此添加一个可执行的文件开机自启动完成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值