Android 系统添加开机自启动脚本(转)

本文详细介绍了如何在Android 8.1的PX30设备上创建开机自启动的日志服务,包括脚本编写、系统编译、服务添加、权限设置等步骤。重点在于如何将脚本编译到系统并确保服务在系统启动后自动运行。

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

本文转载链接:Android 系统添加开机自启动脚本_我的小鱼楠的博客-优快云博客_安卓开机自启脚本

OS:Android8.1
Platform:PX30
我们这儿以创建一个开机自启动日志服务为例

1、创建需要自启动的脚本文件
创建一个名字为init_tool.sh的脚本文件,内容如下:

#!/system/bin/sh

mkdir  -p /sdcard/logs/
logcat -b system -b events -b main -b radio -b kernel -n 10 -v threadtime -f /sdcard/logs/alllog -r5000

脚本名称及内容可自定义。
2、将脚本文件编译至系统,并放置在系统的system/bin目录下。
在device/rockchip/rk3326/device-common.mk文件中添加

# add for log inittool.sh
PRODUCT_COPY_FILES += \
       $(TARGET_DEVICE_DIR)/othertool/init_tool.sh:system/bin/init_tool.sh



其中$(TARGET_DEVICE_DIR)/othertool/init_tool.sh 这个是脚本在SDK的路径,可自定义。system/bin/init_tool.sh这个是脚本拷贝至系统的路径,需固定为system/bin。
完成后编译代码,可在out/target/product/rk3326_evb/system/bin/查看是否修改成功,如没有init_tool.sh 则需检查文件修改是否正确。

3、添加系统启动服务
在device/rockchip/common/init.rockchip.rc中添加

service inittool /system/bin/sh /system/bin/init_tool.sh
    #class core
    user root
    group root
    disabled
    oneshot
    seclabel u:r:inittool:s0



init.rc 的语法可自行网上查阅,因设计较多,这儿就不一一讲解,需要用到的时候再去查相关用法。

4、设置服务开机自启动
在device/rockchip/common/init.rk30board.rc中添加

@@ -249,7 +249,12 @@ on property:sys.boot_completed=1
     setprop persist.sys.first_booting false
     # if data is encrypt, when start gps too early, gpsd crashed
     start gpsd
-
+#Add INIT_TOOL SERVICE
+    wait /sdcard/Android
+    start inittool

注意,这儿启动服务一定要有on property:sys.boot_completed=1即等待系统启动完成后,再启动inittool服务。


5、给服务添加系统权限
在device/rockchip/common/sepolicy/file_contexts中添加

#for log tool
/system/bin/init_tool.sh                       u:object_r:inittool_exec:s0



device/rockchip/common/sepolicy/init.te中添加

#init tool
allow init inittool_exec:file  { getattr };



创建inittool.te放置在device/rockchip/common/sepolicy文件夹下
内容为

type inittool, domain;
type inittool_exec, exec_type, file_type;
#init_daemon_domain(rftoolcopy)

allow inittool shell_exec:file { getattr open read };
#allow rftoolcopy toolbox_exec:file { getattr execute read open execute_no_trans };
allow inittool storage_file:lnk_file { read   };
allow inittool tmpfs:dir { search  };
allow inittool mnt_user_file:dir { search  };
allow inittool mnt_user_file:lnk_file {  read };


注意:

inittool.te 这个文件名称需与上面创建的服务名称一致(上面创建的服务名称为inittool,即我们的权限文件为inittool.te),包括文件内容的名字,都需要以第三点创建的服务名称为标准,不可自定义命名

至此,我们的开机服务已经添加完成,重新编译系统,刷机后可验证服务已一直启动在后台,如需杀掉该服务,可使用kill 命令清理掉后台服务。但每次重启后都会再次启动。如不需要使用了,需在代码里设置关闭。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值